簡體   English   中英

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

[英]Macro that populates values in specific column according to another column

我想尋求幫助。 我正在尋找可以執行此操作的宏:

我有一列稱為Comment的列,應根據Comment列填充其他五個列。 如果在“注釋”列中有一條消息“重復”,我想在稱為“重復1”的列中標記為“ 0”。在這種情況下,也會出現兩個值可以表示要填充1的情況-即“左-找到替換項”和“左-不可更換。 對於這兩個值,“左列”中應填充1,其他值應為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

我找到了解決這個問題的方法。 因此,如果有人需要類似的宏,則代碼如下:

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