0

我想寻求帮助。我正在寻找一个可以执行此类操作的宏:

我有一个名为 Comment 的列和其他五个应根据 Comment 列填充的列。如果在评论栏中有一条消息“重复”,我想在名为 Duplicate 1 的栏中标记如果不是,它应该是 0。同样在这种情况下,两个值可以指示要填充 1 - 即 Left - Replacement Found 和左 - 没有更换。对于这两个值,左列中应填充 1,其他值为 0。

我修改了用于 Vlookup 的宏,并且我能够获得 if 函数,但只能用于 1 个特定的逻辑测试(在本例中为“左 - 找到替换”)。我需要进行 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
4

1 回答 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, 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
于 2013-02-21T11:28:16.527 回答