我有兩個工作簿; 「主」和「zControl」。它們分別有一個名爲「Master」和「zControl」的工作表。基於另一個電子表格的值將值添加到電子表格
如果在「zControl」的「A」列和「Master」的「M」列中找到特定的短語,我需要向「Master」的列「N」添加一個字符串。
「Master」的列「M」可能包含比「zControl」的列「A」中的短語更多的信息,因此它不會完全匹配,但將包含列「A 「在」zControl「中按順序排列,並用空格分隔單詞。
要添加到「Master」列「N」的字符串需要從「zControl」的列「B」的單元格中拉出,該列與「A」列中的匹配值位於同一行中的「zControl」。
的什麼,我需要用通俗易懂的語言例子:
如果「zControl」的「A2」相匹配的「大師」,「M44」的一部分,然後添加「zControl」的「B2」爲「N44」的「主」。
查找下一個匹配,並執行相同的邏輯......
的什麼,我需要宏觀通俗易懂的語言做又如:
如果「zControl」的「A49」匹配「的一部分「Master」的「M2」,然後將「zControl」的「B49」添加到「Master」的「N2」中。
查找下一個匹配,並執行相同的邏輯......
這是我到目前爲止有:
Sub Adder()
Dim WS_Master As Worksheet
Dim WS_Control As Worksheet
Dim WS_Master_Lastrow As Long
Dim WS_Control_Lastrow As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set WS_Master = Workbooks("Master").Worksheets("Master")
Set WS_Control = Workbooks("zControl").Worksheets("zControl")
'Find last row of WS_Master
WS_Master_Lastrow = WS_Master.Cells.Find("*", [A1], , , xlByRows,
xlPrevious).Row
'find last column of WS_Master
WS_Master_Lastcol = WS_Master.Cells.Find("*", [A1], , , xlByColumns,
xlPrevious).Column
'Find last row of WS_Control
WS_Control_Lastrow = WS_Control.Cells.Find("*", [A1], , , xlByRows,
xlPrevious).Row
'Add control sheet data to a array
Dim ControlData() As String
ReDim ControlData(1 To WS_Control_Lastrow, 1 To 3)
For i = 1 To WS_Control_Lastrow
ControlData(i, 1) = Trim(WS_Control.Range("A" & i).Value)
ControlData(i, 2) = Trim(WS_Master.Range("M" & i).Value)
ControlData(i, 3) = Trim(WS_Control.Range("B" & i).Value)
Next i
'Loop through Master sheet
For i = 1 To WS_Master_Lastrow
'Loop through Control sheet
For j = 2 To WS_Control_Lastrow
'if both string found
If InStr(1, WS_Control.Range("A" & i).Value, ControlData(j, 1),
vbTextCompare) > 0 And _
InStr(1, WS_Master.Range("M" & i).Value, ControlData(j, 2),
vbTextCompare) > 0 Then
'Add Save Value to the row
For k = 1 To WS_Master_Lastrow
If WS_Master.Cells(i, j).Value <> "" Then
WS_Master.Range("N" & j).Value = WS_Control.Cells(i, 2)
End If
Next k
Exit For
End If
Next j
Next i
MsgBox "Completed!", vbInformation, ""
End Sub
在這一點上,我收到的IF語句掛了。我幾乎可以肯定,錯誤的原因是「.Range」參數,但無法弄清楚如何修復它,以便宏將必要的值添加到列「N」中正確的單元格。
這是我在VBA中做的最複雜的事情,也是我第一次使用數組。我提到的原因是,即使沒有這個錯誤,我可能不會做我想做的事情。
任何想法或方向將不勝感激。
感謝您花時間閱讀本文,並且如果能夠的話,請提供您的意見。
編輯:
這是我現在在哪裏。
Sub Adder()
Dim WS_Master As Worksheet 'This was GNC
Dim WS_Control As Worksheet 'This was the ControlSheet.
Dim WS_Master_Lastrow As Long
Dim WS_Control_Lastrow As Long
Dim i As Long
Dim j As Long
Dim k As Long
Set WS_Master = Workbooks("Master").Worksheets("Master")
Set WS_Control = Workbooks("zControl").Worksheets("zControl")
'Find last row of WS_Master
WS_Master_Lastrow = WS_Master.Cells.Find("*", [A1], , , xlByRows,
xlPrevious).Row
'find last column of WS_Master
WS_Master_Lastcol = WS_Master.Cells.Find("*", [A1], , , xlByColumns,
xlPrevious).Column
'Find last row of WS_Control
WS_Control_Lastrow = WS_Control.Cells.Find("*", [A1], , , xlByRows,
xlPrevious).Row
'Add control sheet data to a array
Dim ControlData() As String
ReDim ControlData(1 To WS_Control_Lastrow, 1 To 3)
For i = 1 To WS_Control_Lastrow
ControlData(i, 1) = Trim(WS_Control.Range("A" & i).Value)
ControlData(i, 2) = Trim(WS_Master.Range("M" & i).Value)
ControlData(i, 3) = Trim(WS_Control.Range("B" & i).Value)
Next i
'Loop through data sheet
For i = 1 To WS_Master_Lastrow
'Loop through control sheet
For j = 2 To WS_Control_Lastrow
'if both string found
If InStr(1, WS_Control.Range("A" & i).Value, ControlData(j, 1),
vbTextCompare) > 0 And _
InStr(1, WS_Master.Range("M" & i).Value, ControlData(j, 2),
vbTextCompare) > 0 Then
'Add Save Value to the row
WS_Master.Range("N" & j).Value = WS_Control.Cells(i, 2)
End If
Next j
Next i
MsgBox "Completed!", vbInformation, ""
End Sub
所有這一切都做的是「B2」從「zControl」的值增加的「主」,「zControl」的「主」,「N2」,「B3」爲「N3」等...
任何其他想法將不勝感激。正如之前所暗示的,我對VBA非常陌生,並且正在學習。
嘗試範圍(「N」&j)。value = cells(j,3) –
@Sivaprasath完成更改後,在上一行(IF語句的開始)中出現了「運行時錯誤'1004':」「應用程序定義或對象定義的錯誤」。 –
你爲什麼通過列循環K? <如果WS_Master.Cells(i,k).Value <>「」Then>單元格(行索引,列索引)。如果你的行數已經超過了最大列數,它會拋出一個錯誤 –