简体   繁体   English

VBA 代码用于从一个工作表到另一个工作表中搜索列数据并将相应的行数据粘贴到第一个工作表上

[英]VBA code to search for a column data from one sheet to another and paste corresponding rows data onto first sheet

I am new to VBA and i am not able to find any solution to my problem.我是 VBA 的新手,我找不到任何解决问题的方法。 I have two workbooks with data.我有两个包含数据的工作簿。 In workbook1 there is a name column A.In workbook2 there is also a name columnA and other data from column B to D. I need to search name from Column A of workbook2 in column A of workbook 1 and if the name matches I need to paste the corresponding rows in workbook1.在 workbook1 中有一个名称列 A。在 workbook2 中还有一个名称 columnA 和从 B 列到 D 的其他数据。我需要在工作簿 1 的 A 列中从 workbook2 的 A 列中搜索名称,如果名称匹配我需要在 workbook1 中粘贴相应的行。 Also please note that In workbook2 there may be more than one entry for same name.. so in those cases those row values must be concatenated and pasted on workbook1.另请注意,在工作簿 2 中,同名的条目可能不止一个。因此,在这些情况下,必须将这些行值连接并粘贴到工作簿 1 上。

Please help请帮忙

Dim AVals As New Dictionary Dim k As Long, j As Long, lastRow1 As Long, lastRow2 As Long Dim sh_1, sh_3 As Worksheet Dim MyName As String Dim tmpCollection As Collection Set sh_1 = Sheets("snipe-sample-assets blank") Dim key As Variant Dim AVals 作为新字典 Dim k As Long, j As Long, lastRow1 As Long, lastRow2 As Long Dim sh_1, sh_3 As Worksheet Dim MyName As String Dim tmpCollection As Collection Set sh_1 = Sheets("snipe-sample-assets blank") Dim键作为变体

inputRowMin = 1
inputRowMax = 288
inputColMin = 1
inputColMax = 9
equipmentCol = 4
dimensionCol = 9

Set equipmentDictionary = CreateObject("Scripting.Dictionary")
equipmentDictionary.CompareMode = vbTextCompare
Set inputSheet = Application.Sheets("Verizon WirelessNumbers_2021033")
Set inputRange = Range(Cells(inputRowMin, inputColMin), Cells(inputRowMax, inputColMax))
Set equipmentCollection = New Collection

For i = 1 To inputRange.Height
    thisEquipment = inputRange(i, equipmentCol).Text
    nextEquipment = inputRange(i + 1, equipmentCol).Text
    thisDimension = inputRange(i, dimensionCol).Text

    'The Strings are equal - add thisEquipment to collection and continue
    If (StrComp(thisEquipment, nextEquipment, vbTextCompare) = 0) Then
        equipmentCollection.Add thisDimension
    'The Strings are not equal - add thisEquipment to collection and the collection to the dictionary
    Else
        equipmentCollection.Add thisDimension
        equipmentDictionary.Add thisEquipment, equipmentCollection
        Set equipmentCollection = New Collection
    End If

Next

'Set sh_3 = Sheets("sheet2")

lastRow2 = sh_1.Range("A:A").Rows.Count
lastRow2 = sh_1.Cells(lastRow2, 2).End(xlUp).Row 'last used row in column 2
'MsgBox lastRow2

For j = 2 To lastRow2
    MyName = UCase(sh_1.Cells(j, 2).Value)
    For Each key In equipmentDictionary.Keys
        If (StrComp(MyName, key, vbTextCompare) = 0) Then
            Set tmpCollection = equipmentDictionary.Item(MyName)
            For k = 1 To tmpCollection.Count
                sh_1.Cells(j, 10).Value = tmpCollection.Item(k)
            Next
        End If
        
    Next
    
Next j

Quick run down of what you need快速满足您的需求

'You declare all these based on where your data resides
sheetName1 = "Sheets1"
sheetName2 = "Sheets2"
wbName1 = activeworkbook.name
wbName2 = activeworkbook.name   'I've included this for where you might want to fork solution to work off two workbooks

'Loop through entries in sheetName1
iRows1 = 1
Do Until IsEmpty(workbooks(wbName1).sheets(sheetName1).cells(iRows1,1))
    sourceName = workbooks(wbName1).sheets(sheetName1).cells(iRows1,1)

    'Loop through entries in sheetName2
    colB = ""
    colC = ""
    colD = ""
    iRows2 = 1
    Do Until IsEmpty(workbooks(wbName2).sheets(sheetName2).cells(iRows2,1))
        if workbooks(wbName2).sheets(sheetName2).cells(iRows2,1) = sourceName then
            'If there is a match then append. If you want to delimit, then you'd need to add in a delimiter & "," for example
            colB = colB & workbooks(wbName2).sheets(sheetName2).cells(iRows2,2).text
            colC = colC & workbooks(wbName2).sheets(sheetName2).cells(iRows2,3).text
            colD = colD & workbooks(wbName2).sheets(sheetName2).cells(iRows2,4).text
        end if
        iRows2 = iRows2 + 1
    Loop

    if colB <> "" then
        'Found something, send it to sheetName1
        workbooks(wbName1).sheets(sheetName1).cells(iRows1,2) = colB
        workbooks(wbName1).sheets(sheetName1).cells(iRows1,3) = colC
        workbooks(wbName1).sheets(sheetName1).cells(iRows1,4) = colD
    end if

    iRows1 = iRows1 + 1
Loop

If you want to do it for separate workbooks, then you need to assign a wbName2.如果要为单独的工作簿执行此操作,则需要分配一个 wbName2。 My use of ActiveWorkbook assumes it'll be run out of the workbook you are pasting to.我对 ActiveWorkbook 的使用假定它将用完您要粘贴到的工作簿。 It also assumes you've opened both workbooks.它还假设您已经打开了两个工作簿。 You can figure that bit out yourself I'm sure.我敢肯定,您可以自己弄清楚这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于将数据从第 2 行复制到一张工作表上的最后一行数据并粘贴到另一张工作表的第一空行的 VBA 代码 - VBA Code to Copy Data from Row 2 to Last Row of Data on One Sheet and Paste to the First Empty Row of Another Sheet VBA将相应的数据复制并粘贴到新工作表上 - VBA copy and paste corresponding data onto new sheet 如何使用vba将多列数据从一张工作表复制并粘贴到另一张工作表 - How to copy and paste multiple column data from one sheet to another sheet using vba 从一张纸复制数据并粘贴到另一张纸的A列的第一个空白单元格中 - Copy data from one sheet and paste into the first blank cell in column A of another sheet 如何清除工作表并将数据从另一工作表粘贴到工作表上 - How to clear a sheet and paste data onto it from another sheet 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA 每个循环的VBA,可将数据从一张纸粘贴到另一张纸的底部 - VBA for each loop to paste data from one sheet to bottom of another VBA代码将某些数据从工作表复制并粘贴到另一个 - VBA code to copy and paste certain data from sheet to another 如何使用列名从一张工作表复制数据并粘贴到具有相同列名的另一张工作表? - How to copy data from one sheet using column name and paste to another sheet with same column name? 修复 vba 将行数据从一列复制到另一个工作表 - Fixing vba copy rows data from a column to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM