【问题标题】:Matching Data Between Two Workbooks and Paste Data Based on在两个工作簿之间匹配数据并粘贴数据基于
【发布时间】:2020-12-09 09:29:16
【问题描述】:

我正在处理的情况是一个表格,其中第一列是帐号,第五列是金额,第七列是“F”或“P”。帐号与位于第一列中另一个工作簿上的帐号相匹配。如果在表格的第七列(在源工作簿中,有一个“F”,则该值应该被复制、匹配并粘贴到目标工作簿第四列的同一行。如果有一个“ P,' 值应匹配并粘贴到目标工作簿第五列的同一行。代码有效,但无法区分 F 或 P。它将所有值都粘贴到两列中。

Private Sub CommandButton2_Click()
Dim Dic As Object, key As Variant, oCell As Range, i&
Dim w1 As Worksheet, w2 As Worksheet
Dim cell As Range
Dim SrchRng As Range

Set Dic = CreateObject("Scripting.Dictionary")
Set w1 = Workbooks("HF Pricing Template1").Sheets("Tables")
Set w2 = Workbooks("Book1").Sheets("Sheet1")
Set SrchRng = Range("Table3[Price_Type]")

For Each cell In SrchRng
If cell.Value = "P" Then

i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row

    For Each oCell In w1.Range("M5:M" & i)
        If Not Dic.exists(oCell.Value) Then
            Dic.Add oCell.Value, oCell.Offset(, 5).Value
        End If
    Next


 i = w2.Cells.SpecialCells(xlCellTypeLastCell).Row

    For Each oCell In w2.Range("A2:A" & i)
        For Each key In Dic
            If oCell.Value = key Then
                oCell.Offset(, 3).Value = Dic(key)
            End If
        Next
    Next
End If

Next cell
For Each cell In SrchRng
If cell.Value = "P" Then

   i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row

    For Each oCell In w1.Range("M5:M" & i)
        If Not Dic.exists(oCell.Value) Then
            Dic.Add oCell.Value, oCell.Offset(, 5).Value
        End If
    Next


    i = w2.Cells.SpecialCells(xlCellTypeLastCell).Row

    For Each oCell In w2.Range("A2:A" & i)
        For Each key In Dic
            If oCell.Value = key Then
                oCell.Offset(, 4).Value = Dic(key)
            End If
        Next
    Next
 End If

 Next cell

End Sub

【问题讨论】:

    标签: excel vba matching


    【解决方案1】:

    这次完全重写怎么样。

    阅读 cmets 以获得解释

    'compare text (ignore case)
    option compare text 
    
    dim source_wb as workbook
    dim dest_wb as workbook
    
    dim source_ws as worksheet
    dim dest_ws as worksheet
    
    'set workbooks/sheets
    set source_wb = workbooks("HF Pricing Template1")
    set source_ws = source_wb.worksheets("Table")
    
    set dest_wb = workbooks("Book1")
    set dest_ws = dest_wb.worksheets("Sheet1")
    
    dim source_lr as integer
    dim dest_lr as integer
    
    'get last row of data in each sheet for column 1 (the account numbers)
    'checks for account number list in column "a" change where applicable
    source_lr = source_ws.cells(rows.count, "M").end(xlup).row
    dest_lr = dest_ws.cells(rows.count, "A").end(xlup).row
    
    
    'this starts checking for account numbers at row 2 change where applicable
    for source_row = 2 to source_lr
        ''this start checking for account numbers at row 2 change where applicable
        for dest_row = 2 to dest_lr
            'check if account numbers match
            ' change column as applicable 
            if source_ws.cells(source_row, "M") = dest_ws.cells(dest_row, "A") then
                'if column 7  in source contains p then copy to column 4 in dest ws
                'change column where applicable
                if source_ws.cells(source_row, "S") = "p" then
                    dest_ws.cells(dest_row,"D") = source_ws.cells(source_row, "R")
                    exit for
      
                'if column 7  in source contains f then copy to column 5 in dest ws
                ' change column where applicable
                elseif source_ws.cells(source_row, "S") = "f" then
                    dest_ws.cells(dest_row, "E") = source_ws.cells(source_row, "R")
                    exit for
                end if
            end if
        next dest_row
    next source_row
    

    请注意 - 我不在 Windows 机器上,现在无法测试它,但它应该可以按预期工作。

    【讨论】:

    • 我该如何感谢你?我对您的惊人代码只有一个问题。源工作簿有一个范围为 M5 到 S17 的表。这个逻辑是否捕捉到了这一点?
    • 我已将其修改为有列字母,您可以在适用的地方更改
    • 它的作用是检查表格的长度,因此它可以是任意长度,但您需要自己输入列字母以匹配您的工作表
    • 嗯,源工作簿上的数据位于 M5:S17 之间的表上。该表包含七列。 “M”列包含帐号,应与目标工作簿上的“A”列匹配。 “R”列包含我们试图复制的值,“S”列包含 P 或 F 字母。我们需要宏在“M”列中查看源工作簿并将其与目标匹配并复制“R”列中的值,并且根据 F 或 P,它应该复制第三列或第四列中的同一行分别。
    • 试试那个修正
    【解决方案2】:
    Dim source_wb As Workbook
    Dim dest_wb As Workbook
    
    Dim source_ws As Worksheet
    Dim dest_ws As Worksheet
    
    'set workbooks/sheets
    Set source_wb = Workbooks("HF Pricing Template1")
    Set source_ws = source_wb.Worksheets("Tables")
    
    Set dest_wb = Workbooks("Book1")
    Set dest_ws = dest_wb.Worksheets("Sheet1")
    
    Dim source_lr As Integer
    Dim dest_lr As Integer
    
    'get last row of data in each sheet for column 1 (the account numbers)
    'checks for account number list in column "a" change where applicable
    source_lr = source_ws.Cells(Rows.Count, "M").End(xlUp).Row
    dest_lr = dest_ws.Cells(Rows.Count, "A").End(xlUp).Row
    
    
    'this starts checking for account numbers at row 2 change where applicable
    For source_row = 5 To source_lr
    ''this start checking for account numbers at row 2 change where applicable
    For dest_row = 2 To dest_lr
        'check if account numbers match
        ' change column as applicable
        If source_ws.Cells(source_row, "M") = dest_ws.Cells(dest_row, "A") Then
            'if column 7  in source contains p then copy to column 4 in dest ws
            'change column where applicable
            If source_ws.Cells(source_row, "S") = "P" Then
                dest_ws.Cells(dest_row, "D") = source_ws.Cells(source_row, "M")
                Exit For
            'if column 7  in source contains f then copy to column 5 in dest ws
            ' change column where applicable
            ElseIf source_ws.Cells(source_row, "S") = "F" Then
                dest_ws.Cells(dest_row, "E") = source_ws.Cells(source_row, "M")
                Exit For
    
            End If
         End If
       Next
     Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      相关资源
      最近更新 更多