[英]VBA code to find an item in a specific range and copy to another sheet
I want the code to find a P/N in a list which is on Sheet3.我希望代码在 Sheet3 上的列表中找到 P/N。 Once the P/N is found in that range, its corresponding global P/N, brand, description and list price which are all on the same row as P/N has to be copied and added to Sheet 2 under similar headings.一旦在该范围内找到 P/N,就必须将与 P/N 位于同一行的对应的全球 P/N、品牌、描述和标价复制并添加到表 2 的类似标题下。 The P/N will already be added in Sheet 2. The below is the code that I have tried for this. P/N 已经添加到表 2 中。下面是我为此尝试过的代码。 But, there is a compile error coming as "Next without For" even though Next is added for all the corresponding For in the code.但是,即使在代码中为所有相应的 For 添加了 Next,也会出现编译错误,显示为“Next without For”。
Sub Price()
Dim pno As Double
Dim LastRow As Long
Dim i As Integer
Dim LastRowinMainSheet As Integer
Dim j As Integer
LastRowinMainSheet = Cells.Find(What:="*", _
After:=Range("E23"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRow = Worksheets(3).UsedRange.SpecialCells(x1CellTypeLastCell).Row
For j = 24 To LastRowinMainSheet
Worksheets(2).Cells(j, 5).Value = pno
For i = 3 To LastRow
If Worksheets(3).Cells(i, 2).Value = pno Then
Worksheets(3).Cells(i, 3).Copy
Worksheets(2).Cells(j, 4).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 4).Copy
Worksheets(2).Cells(j, 6).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 5).Copy
Worksheets(2).Cells(j, 7).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 14).Copy
Worksheets(2).Cells(j, 12).PasteSpecial xlPasteValues
Next i
Next j
End Sub
You are missing end if如果你错过了结束
That'll have the same error msg within VBA.这将在 VBA 中具有相同的错误消息。
edit 2021-03-25A Alright, so without knowing the exact shape of your data, I think your looking something like this:编辑 2021-03-25A 好的,所以在不知道数据的确切形状的情况下,我认为您看起来像这样:
Sub Price()
Dim pno As Double
Dim LastRow As Long
Dim i As Integer
Dim LastRowinMainSheet As Integer
Dim j As Integer
LastRowinMainSheet = Worksheets(2).Cells(Worksheets(2).Rows.Count, "E").End(xlUp).Row
LastRow = Worksheets(3).Cells(Worksheets(2).Rows.Count, "E").End(xlUp).Row
For j = 24 To LastRowinMainSheet
pno = Worksheets(2).Cells(j, 5).Value
For i = 3 To LastRow
If Worksheets(3).Cells(i, 2).Value = pno Then
Worksheets(3).Cells(i, 3).Copy
Worksheets(2).Cells(j, 4).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 4).Copy
Worksheets(2).Cells(j, 6).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 5).Copy
Worksheets(2).Cells(j, 7).PasteSpecial xlPasteValues
Worksheets(3).Cells(i, 14).Copy
Worksheets(2).Cells(j, 12).PasteSpecial xlPasteValues
End If
Next i
Next j
End Sub
This work on my machine.这在我的机器上工作。
Beware that the entries for pno [col 5 in sheets(2) and col 2 in sheets(3)] currently must be numbers.请注意 pno [表 (2) 中的第 5 列和表 (3) 中的第 2 列] 的条目当前必须是数字。
In your code, swapping:在您的代码中,交换:
LastRowinMainSheet = Cells.Find(What:="*", _
After:=Range("E23"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
to至
LastRowinMainSheet = Worksheets(3).Cells.Find(What:="*", _
After:=Range("E23"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
also worked for me.也为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.