根据另一列填充特定列中的值的宏

时间:2013-02-20 16:56:24

标签: excel vba excel-vba

我想请求帮助。我正在寻找一个能做这种行动的宏:

我有一个名为Comment的列和其他五个列,应该根据Comment列进行填充。如果在“注释”列中有一条消息“重复”,我想在名为Duplicate 1的列中标记,如果不是,则它应该为0.此外,在这种情况下,两个值可以指示1要填充 - 即左 - 替换找到并且左 - 没有替换。对于这两个值,它应该在左列中填充1,对于其他值0。

我修改了我对Vlookup的宏,我能够得到if函数,但只能用于1个特定的逻辑测试(在这个例子中#34; Left - Replacement found)。我需要进行超过1次逻辑测试。所以在这种情况下,我想让Left - Replacement Found和Left - No Replacement标记为1,其余为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

1 个答案:

答案 0 :(得分: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, 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