简体   繁体   English

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

[英]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. 我有一列称为Comment的列,应根据Comment列填充其他五个列。 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. 如果在“注释”列中有一条消息“重复”,我想在称为“重复1”的列中标记为“ 0”。在这种情况下,也会出现两个值可以表示要填充1的情况-即“左-找到替换项”和“左-不可更换。 For both of those values It should be 1 populated in the Left Column, for others 0. 对于这两个值,“左列”中应填充1,其他值应为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. 我修改了用于Vlookup的宏,但我只能获取if函数,但只能进行1个特定的逻辑测试(在本示例中为“发现左-替换)。我需要进行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,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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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