【问题标题】:If column "a" in sheet1 matches column "a" in sheet2 then replace entire row in sheet1如果 sheet1 中的列“a”与 sheet2 中的列“a”匹配,则替换 sheet1 中的整行
【发布时间】:2022-02-16 08:37:12
【问题描述】:

我想将 sheet1 中的 A 列与 sheet2 中的 A 列进行比较。

如果值匹配,将数据从 sheet2 复制到 sheet1 到匹配值的行中。

示例:

【问题讨论】:

  • 到目前为止你尝试过什么代码?你在哪里遇到了麻烦?请在您的问题中包含这一点。
  • 嗨,我只能编写代码来替换特定的单元格/列,但这完全不能作为 sheet2 中的列(我想从中复制数据的工作表)与 sheet1 中的顺序不同(我想从 sheet2 复制数据的工作表)。
  • 顺序无关紧要,因为您可以按名称和索引来引用工作表。 (取决于你的代码)
  • 在问题中,您说“如果 A 列 (sheet1) 中的值与 B 列 (sheet2) 中的值匹配”,但标题仅提及 A 列。因此,如果它们匹配,则要复制 B 列,C,D,G 到 B,C,D,E
  • @CDP1802 是的,确实如此。如果 A 列(sheet1)中的数据与 A 列(sheet2)中的数据匹配,我想复制整行(但仅用于匹配标题,如果这有意义的话?)。例如,sheet2 的列比 sheet1 的多,所以我想跳过这些“额外”列,因为这些是不需要的。孔板大约有 1,5k 行,到目前为止我一直在手动进行更改....

标签: excel vba


【解决方案1】:

使用字典对象将 sheet2 上的标题名称与 sheet1 上的标题名称匹配。

更新 - 添加修剪以将数字转换为字符串

Option Explicit

Sub Update()

    Const ROW_HEADER = 1

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lastrow As Long, lastcol As Long, r As Long, c As Long
    Dim arID, id As String, n As Long, m As Variant
    
    Dim dictCol As Object, k As String
    Set dictCol = CreateObject("Scripting.Dictionary")
   
   ' profile sheet2 columns
    Set ws2 = ThisWorkbook.Sheets("Sheet2")
    With ws2
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        arID = .Range("A1:A" & lastrow).Value2 ' range of iDs
        
        lastcol = .Cells(ROW_HEADER, .Columns.Count).End(xlToLeft).Column
        For c = 1 To lastcol
            k = Trim(.Cells(ROW_HEADER, c)) ' header text
            If dictCol.exists(k) Then
                MsgBox "Duplicate header '" & k & "' at column " & c, vbCritical
                Exit Sub
            ElseIf Len(k) > 0 Then
                dictCol(k) = c ' column number
            End If
        Next
    End With

    For r = 1 To UBound(arID): arID(r, 1) = Trim(arID(r, 1)): Next
    MsgBox dictCol.Count & " columns found on sheet " & ws2.Name, vbInformation
    
    ' update sheet1
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    With ws1
        lastcol = .Cells(ROW_HEADER, .Columns.Count).End(xlToLeft).Column
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For r = 2 To lastrow
            id = Trim(.Cells(r, "A"))
            
            ' locate row on sheet2
            m = Application.Match(id, arID, 0)
            If Not IsError(m) Then
                ' scan columns
                For c = 2 To lastcol
                    k = trim(.Cells(ROW_HEADER, c))
                    ' find col on sheet2
                    If dictCol.exists(k) Then
                        ' update if different
                        If .Cells(r, c) <> ws2.Cells(m, dictCol(k)) Then
                            .Cells(r, c).Interior.Color = RGB(255, 255, 0) ' mark yellow for checking
                            .Cells(r, c) = ws2.Cells(m, dictCol(k))
                            n = n + 1
                         End If
                    Else
                        MsgBox "Column " & k & " not found", vbCritical
                        Exit Sub
                    End If
                Next
            Else
                Debug.Print id, m
            End If
        Next
    End With
    
    ' end
    MsgBox lastrow - 1 & " rows scanned " & vbLf & _
           n & " cells updated", vbInformation
   
End Sub

【讨论】:

  • 可爱,我将在大约 2 小时内尝试您的代码,如果可行,我会通知您并将其标记为已解决 :) 感谢您的所有帮助。
  • 你能告诉我我做错了什么吗?我已经在模块中使用了您的代码,当我启动它时,我收到一条消息“在 sheet2 上找到 24 列(sheet2 中的列数),我只能单击“确定”,一旦我这样做,第二个弹出窗口显示“扫描了 509 行,更新了 0 个单元格”(sheet1 上的 509 行)。我相信代码运行良好,但它不会替换 sheet1 中单元格中的数据。
  • 第一行用于标题(A1 = 员工 ID,B1= 资源名称)等。A 列一直向下是员工 ID(两张表)。
  • 检查,在任何列标题中都没有多余的空格(除了单词之间的正常空格),并且在我有员工 ID 的列 A 中根本没有空格
  • @Marcin 在 VB 编辑器的即时窗口中显示的内容,是否有带有错误 2042 的 ID 列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
  • 2022-12-01
相关资源
最近更新 更多