简体   繁体   English

VBA 代码查找特定范围内的项目并复制到另一张表

[英]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.

相关问题 Excel VBA-复制到特定工作表的代码中的错误,具体取决于范围 - Excel VBA - Error in code to copy to specific sheet, dependent on range excel vba-特定的复制/粘贴字符串到另一个工作表,其所有范围都在另一个工作表中的特定范围 - excel vba - specific copy/paste string to another sheet with all its range to specific range in another worksheet 用于将多个范围复制到另一个工作簿中的特定工作表的 Excel VBA 代码 - Excel VBA code to copy several ranges to specific sheet in another workbook 在范围内找到特定的字符串并将其复制到VBA中的另一个字段 - Find a specific String in a range and copy it to another field in VBA 来自另一张工作表的Excel VBA范围副本 - Excel VBA range copy from another sheet 来自另一张纸的VBA复制范围 - VBA copy range from another sheet VBA复制范围值并粘贴到另一张工作表中 - VBA Copy range values and paste in another sheet VBA-将特定标题从工作表复制到另一个 - VBA - Copy a specific headers from a sheet to another Excel VBA在工作表中查找文本,复制范围,粘贴到其他工作表 - Excel vba find text in sheet, copy range, paste to other sheet 试图将特定范围从工作簿中的许多工作表复制到另一个工作簿中的一个工作表 vba excel? - Trying to copy specific range from many sheets in workbook to one sheet in another workbook vba excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM