Sub MatchLiquor()
Dim lastRowOne As Long
Dim lastRowTwo As Long
Dim sheetNameOne As String
Dim sheetNameTwo As String
sheetNameOne = "Recipies" 'Recipies Sheet
sheetNameTwo = "Liquor Breakdowns" 'Liquor Breakdowns
lastRowOne = Sheets(sheetNameOne).Range("B" & Rows.Count).End(xlUp).Row
lastRowTwo = Sheets(sheetNameTwo).Range("A" & Rows.Count).End(xlUp).Row
For lRow = 2 To lastRowOne 'Loop through all rows
For lRowTwo = 2 To lastRowTwo 'Loop through all rows
If Sheets(sheetNameOne).Cells(lRow, "B") = Sheets(sheetNameTwo).Cells(lRowTwo, "A") Then
Sheets(sheetNameOne).Cells(lRow, "C") * Sheets(sheetNameOne).Cells(lRowTwo, "E") Then
End If
Next lRowTwo
Next lRow
End Sub
在sudo中,我试图完成的是这样的: 在" name"中搜索所有的sheetA行并与sheetB" name"进行比较 找到后,将sheetA中不同的值乘以sheetB中的不同值;将值输出到sheetA中的不同单元格(示例E)。
我还没有弄清楚如何输出,或者即使这会起作用。 想法?
答案 0 :(得分:0)
关于哪些工作表被使用的地方存在一些混淆,并且在匹配时没有提供产品的目标,但这应该清理你的循环。
Sub MatchLiquor()
Dim lastRowOne As Long, lastRowTwo As Long, prodct As Double
Dim sheetNameOne As String, sheetNameTwo As String
sheetNameOne = "Recipies" 'Recipies Sheet
sheetNameTwo = "Liquor Breakdowns" 'Liquor Breakdowns
With Sheets(sheetNameOne)
lastRowOne = .Range("B" & Rows.Count).End(xlUp).Row
lastRowTwo = Sheets(sheetNameTwo).Range("A" & Rows.Count).End(xlUp).Row
For lRow = 2 To lastRowOne 'Loop through all rows on Recipies Sheet
For lRowTwo = 2 To lastRowTwo 'Loop through all rows on Liquor Breakdowns
If .Cells(lRow, "B") = Sheets(sheetNameTwo).Cells(lRowTwo, "A") Then
prodct = .Cells(lRow, "C").Value * Sheets(sheetNameTwo).Cells(lRowTwo, "E").Value
'do something with prodct
.cells(lRow, "E") = prodct
Exit For 'found match and computed product; no need to continue this lRowTwo
End If
Next lRowTwo
Next lRow
End With
End Sub
如果找到匹配项并计算产品(并放置在某处),则无需继续使用lRowTwo
运行内部嵌套循环。只需Exit For
即可返回外部循环和下一个lRow
。
在With ... End With statement中使用Sheets(sheetNameOne)
会将父工作表引用减半。