【问题标题】:Issue with data match数据匹配问题
【发布时间】:2015-11-20 10:36:49
【问题描述】:

让我的循环正确跳过记录时遇到问题。我有多个记录需要从许多从工作簿中更新。每个奴隶都有一个不同的名称(它的用户),我需要通过只比较那些包含用户名称的记录,然后比较唯一的 ID,而不是它当前所做的,来使我的“更新”循环更有效,这是比较所有唯一 ID 以找到匹配项。

我当前的命令按钮只是在列中查找用户名,如果计数高于 0,它会调用模块来更新该用户的记录。

在 Master 和 Slave 中,第 1 列始终是记录的唯一 ID,第 2 列始终是记录分配给的用户。这是我目前的测试编码(将成为其他用户工作簿的模板):

Option Explicit
Public Sub Agnes_Update()

 Dim owb As Workbook
 Dim Master, Slave As Worksheet
 Dim fpath As String
 Dim i, j As Integer

fpath = Application.ActiveWorkbook.Path & "\Agnes.xlsx"

Set owb = Application.Workbooks.Open(fpath)
Set Master = ThisWorkbook.Worksheets("Allocated")
Set Slave = owb.Worksheets("Work")


For j = 2 To 10 '(the master sheet)

For i = 2 To 10 '(the slave sheet)
    'if ID cell is blank exit - ends loop when all updates are completed
    If Trim(Slave.Cells(j, 1).Value2) = vbNullString Then Exit For 
    'if column 2 of master does not contain the current username being 
    'updated then move to next record
    If Master.Cells(j, 2).Value = "Agnes" Then  
    'if unique ID in column 1 matches slave from master then begin 
    'updating of required cells
    If Master.Cells(i, 1).Value2 = Slave.Cells(j, 1).Value2 Then
        Master.Cells(i, 4).Value = Slave.Cells(j, 4).Value
        Master.Cells(i, 5).Value = Slave.Cells(j, 5).Value
        Master.Cells(i, 6).Value = Slave.Cells(j, 6).Value
        Master.Cells(i, 7).Value = Slave.Cells(j, 7).Value
        Master.Cells(i, 8).Value = Slave.Cells(j, 8).Value
        Master.Cells(i, 9).Value = Slave.Cells(j, 9).Value
        Master.Cells(i, 10).Value = Slave.Cells(j, 10).Value
        Master.Cells(i, 11).Value = Slave.Cells(j, 11).Value
        Master.Cells(i, 12).Value = Slave.Cells(j, 12).Value
        Master.Cells(i, 13).Value = Slave.Cells(j, 13).Value
        Master.Cells(i, 14).Value = Slave.Cells(j, 14).Value
        Master.Cells(i, 15).Value = Slave.Cells(j, 15).Value
        Master.Cells(i, 16).Value = Slave.Cells(j, 16).Value
        Master.Cells(i, 17).Value = Slave.Cells(j, 17).Value
        Master.Cells(i, 18).Value = Slave.Cells(j, 18).Value
        Master.Cells(i, 19).Value = Slave.Cells(j, 19).Value
        Master.Cells(i, 20).Value = Slave.Cells(j, 20).Value
        Master.Cells(i, 21).Value = Slave.Cells(j, 21).Value
        Master.Cells(i, 22).Value = Slave.Cells(j, 22).Value
        Master.Cells(i, 23).Value = Slave.Cells(j, 23).Value
    End If
    End If
    Next
Next

Workbooks("Agnes").Close

End Sub

我更喜欢使用Master.cells = Slave.cells的方法,因为一些从单元格被锁定以防止用户修改数据,并且将来某些数据的位置可能会改变列,所以我将简单地修改哪个主列= 哪个从属列。我意识到我可以设置代码来解锁工作簿,但对于简单的更新来说,这需要更多的代码。

我相信代码的当前问题在于 If Master.Cells(j, 2).Value = "Agnes" Then 行,因为删除此行允许代码通过所有唯一 ID 来查找和更新所有匹配的主控,但我宁愿它只尝试当在第 2 列中找到用户名时匹配唯一 ID,以尝试使代码更快、更高效。

谁能帮我改正这段代码?

【问题讨论】:

  • |这里的错误不是很清楚。即使行中的名称是“Agnes”,子程序是否永远不会进入内部If?您是否需要从 Master.Cells(j, 2).Value 中删除前导或尾随空格?
  • 嗨@stucharo 抱歉,我的问题是分配的工作表永远不会更新从属的Agnes 记录,即使它在单元格中,我也无法这样做。跨度>

标签: excel if-statement for-loop updates vba


【解决方案1】:

如果我理解正确,您需要将测试移到内部循环之前(我做了一些其他小的更改以减少代码量并停止重复)

Public Sub Agnes_Update()
Dim owb As Workbook
Dim wbname As String
Dim Master, Slave As Worksheet
Dim fpath As String
Dim i, j As Integer

    fpath = Application.ActiveWorkbook.Path & "\Agnes.xlsx"

    Set owb = Application.Workbooks.Open(fpath)
    wbname = Left$(owb.Name, InStrRev(owb.Name, ".") - 1)
    Set Master = ThisWorkbook.Worksheets("Allocated")
    Set Slave = owb.Worksheets("Work")

    For j = 2 To 10 '(the master sheet)

        'if column 2 of master does not contain the current username being
        'updated then move to next record
        If Master.Cells(j, 2).Value = wbname Then

        For i = 2 To 10 '(the slave sheet)

            'if ID cell is blank exit - ends loop when all updates are completed
            If Trim(Slave.Cells(j, 1).Value2) = vbNullString Then Exit For

                'if unique ID in column 1 matches slave from master then begin
                'updating of required cells
                If Master.Cells(i, 1).Value2 = Slave.Cells(j, 1).Value2 Then

                    Slave.Cells(j, 4).Resize(, 20).Copy
                    Master.Cells(i, 4).PasteSpecial Paste:=xlPasteValues
                End If
            End If
        Next
    Next

    owb.Close SaveChanges:=False
End Sub

【讨论】:

  • 嗨@Bob Phillips,感谢您的回复。我在If Master.Cells(j, 2).Value = wbname Then 上方有If Trim(Slave.Cells(j, 1).Value2) = vbNullString Then Exit For,因为每张纸上的行数可能会有所不同,因此ijFor 将设置为1000,并且vbNullString 如果它提前结束循环行数用完了。这会有所作为吗?或者,还有更好的方法?然而,其余的看起来很棒,我很快就会测试代码。再次感谢!
  • 你好@Wowdude - 你是对的,该语句只引用了 j 变量,而不是 i,所以没有理由将它留在内部循环中。它不应该有太大的不同,因为它会在遇到这种情况时立即退出,但它会避免一次又一次地改变它。好地方,我应该自己发现的:-)
  • 感谢@Bob Phillips - 非常正确,看起来这只是循环的定位 =D
猜你喜欢
  • 2022-09-01
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
相关资源
最近更新 更多