【问题标题】:Is it possible to compare two columns in excel and update the master column using VBA?是否可以比较 excel 中的两列并使用 VBA 更新主列?
【发布时间】:2020-03-19 14:32:00
【问题描述】:

我的 Excel 工作簿中有两张工作表。 这些表中包含的是我的主键列。 我想使用 VBA 循环将第一列(主列)与第二列(源)进行比较。 原因是源通常包含新的主键。 请任何人都可以帮助我找出比较这些列并将唯一值添加到主列的逻辑。 谢谢。

this image shows the sample master code
this image shows the sample source code

下面的代码显示了我到目前为止所拥有的

Sub PullUniques()
Dim rngCell As Range
For Each rngCell In Sheet1.Range("W3:W40")
    If WorksheetFunction.CountIf(Range("D3:D40"), rngCell) = 0 Then
        Range("C" & Rows.Count).End(xlUp).Offset(1) = rngCell
    End If
Next
For Each rngCell In Sheet6.Range("D3:D40")
    If WorksheetFunction.CountIf(Range("W3:W40"), rngCell) = 0 Then
        Range("W" & Rows.Count).End(xlUp).Offset(1) = rngCell
    End If
Next

结束子

【问题讨论】:

  • 如果您能提供数据样本并向我们展示您迄今为止尝试过的 VBA 代码,我们可以更好地回答您的问题。
  • 我们是否应该理解您需要在主表(col A:A)中添加仅存在于源表(col A:A)中但在主表中找不到的值?
  • 是的@FaneDuru 这正是我想要的
  • 我会准备一个答案。但是,如果您不进行一些研究并编辑发布一段代码的问题,则可以关闭您的问题,直到我将其发布...
  • 好的,谢谢。我现在正在编辑它

标签: excel vba loops


【解决方案1】:

请试试这个代码。它基于以下假设:在源工作表中可能存在“主”工作表中不存在的键,这些键将添加到主工作表的第一个空行。

Sub testMasterUpdate()
  Dim shM As Worksheet, shS As Worksheet, s As Long, boolF As Boolean
  Dim lastRM As Long, lastRS As Long, m As Long
  Dim arrM As Variant, arrS As Variant, arrDif As Variant, d As Long

  Set shM = Worksheets("Master") 'please, use here your sheet name
  Set shS = Worksheets("Source") 'please, use here your sheet name
   lastRM = shM.Range("A" & Cells.Rows.Count).End(xlUp).Row
   lastRS = shS.Range("A" & Cells.Rows.Count).End(xlUp).Row
   arrM = shM.Range("A2:A" & lastRM).value
   arrS = shS.Range("A2:A" & lastRS).value
   ReDim arrDif(1 To 1, 1 To UBound(arrM) + UBound(arrS)): d = 1

   For s = 1 To UBound(arrS)
        For m = 1 To UBound(arrM)
            If arrS(s, 1) = arrM(m, 1) Then
                boolF = True
                Exit For
            End If
        Next m
        If Not boolF Then
            arrDif(1, d) = arrS(s, 1)
            d = d + 1
        End If
        boolF = False
   Next s
   If d > 1 Then
     ReDim Preserve arrDif(1 To 1, 1 To d - 1)
     'shM.Range("A" & lastRM + 1).Resize(UBound(arrDif, 2), 1).value = _
                                    WorksheetFunction.Transpose(arrDif)
     shM.Range("A" & lastRM).Resize(UBound(arrDif, 2), 1).value = _
                                WorksheetFunction.Transpose(arrDif)
     lastRM = shM.Range("A" & Cells.Rows.Count).End(xlUp).Row
     shM.Range("A" & lastRM + 1).Formula = "=CountA(A2:A" & lastRM & ")"
   End If
End Sub

请用您的真实名称替换通用工作表名称。

【讨论】:

  • @Philip Ajino:鞠躬,我将离开我的办公室。如果您需要澄清,我将在大约两个小时后回答...
  • 首先,我要感谢您的帮助。当我运行代码并更改值时,它会给出一个运行时错误:“下标超出范围”,当我调试它时,它会突出显示“arrDif(1, d) = arrS(s, 1)”的行.您认为可能导致此错误的原因是什么。
  • 您应该将arrDif 初始化为arrS 的大小,而不是arrM。这可能是 OPs 错误的原因
  • 会更好,但我不认为这是问题所在。看起来 arrDiff 大小不足以解决差异......
  • QPhilip Ajino:我从下一个假设开始准备代码:您有一个主表,其中包含所有“主键”和另一个人们输入新键的主表。除了新代码之外,还有旧代码。但是,情况可能会有所不同......我将调整代码以独立于上述假设工作。解决了。请问您现在可以检查一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多