简体   繁体   中英

Macro that populates values in specific column according to another column

I would like to ask for a help. I'm looking for a macro which would do such action:

I have a column called Comment and five other columns which should be populated according to the Comment column. If in Comment column there is a message “duplicate” I would like to mark in column called Duplicate 1 if not it should be 0. Also in this case it happens that two values can indicate 1 to be populated – ie Left - Replacement Found and Left - No Replacement. For both of those values It should be 1 populated in the Left Column, for others 0.

I modified macro which I have for Vlookup and I was able to get if function but only for 1 specific logical test (in this example "Left - Replacement found). I would need to have more than 1 logical test. So in this case I would like to have Left - Replacement Found and Left - No Replacement marked as 1 and the rest as 0.

Sub Testing_if_function()
Dim FormulaCol As Long
Dim LookupCol As Long
Dim TotalRows As Long
Dim TotalCols As Long
Dim i As Long

Sheets("Data").Select
TotalRows = ActiveSheet.UsedRange.Rows.Count
TotalCols = ActiveSheet.UsedRange.Columns.Count

For i = 1 To TotalCols
    If Cells(1, i).Value = "Test" Then FormulaCol = i
    If Cells(1, i).Value = "Comment" Then LookupCol = i
Next

ActiveCell.FormulaR1C1 = "=IF(RC[-2]=""Left - Replacement Found"",1,0)"
Cells(2, FormulaCol).AutoFill Destination:=Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
With Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
    .Value = .Value
End With

End Sub

I found the solution of this problem. So if anyone will need similar macro, here is the code:

Sub Testing_if_function()
Dim FormulaCol As Long
Dim LookupCol As Long
Dim TotalRows As Long
Dim TotalCols As Long
Dim i As Long

Sheets("Data").Select
TotalRows = ActiveSheet.UsedRange.Rows.Count
TotalCols = ActiveSheet.UsedRange.Columns.Count

For i = 1 To TotalCols
    If Cells(1, i).Value = "Test" Then FormulaCol = i
    If Cells(1, i).Value = "Comment" Then LookupCol = i
Next

ActiveCell.FormulaR1C1 = "=IF(RC[-2]=""Left - Replacement Found"", 1, IF(RC[-2]=""Left - No Replacement"", 1, 0 ))"
Cells(2, FormulaCol).AutoFill Destination:=Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
With Range(Cells(2, FormulaCol), Cells(TotalRows, FormulaCol))
    .Value = .Value
End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM