【问题标题】:How to make the for loop merge new values across sheets?如何使 for 循环跨工作表合并新值?
【发布时间】:2015-04-19 00:00:51
【问题描述】:

有人帮我编写了初始代码,我试图修改它,但它错了。

我需要将电子表格中的工作表 2 与工作表 4 到 10 进行比较,并且如果 e 行或 b 行的值与任何其他行都不匹配。将整行复制到工作表 1 的底部。

这是我目前所拥有的,但该值并未设置为 true,它会在每张纸之后打印。我坚持

 Sub Button13() 'merge

    Dim lastSourceRow As Long, LastTargetRow As Long, allSheets As Long, lastSheet As Long
    Dim source As String, TARGET As Integer
    Dim tempVal As String, tempValE, tempValT
    Dim tRow As Long, lRow As Long, lCol As Long, nRow As Long
    Dim match As Boolean


    source = "Sheet2"
    lastSheet = "10"

    lastSourceRow = Sheets(source).Range("A" & Rows.Count).End(xlUp).Row


    For allSheets = 1 To lastSheet


    TARGET = allSheets
    LastTargetRow = Sheets(TARGET).Range("A" & Rows.Count).End(xlUp).Row

    For lRow = 2 To lastSourceRow     'Loop through Rows on currentsheet
        Count = "0"
        match = False                 'Reset boolean test for each new row
        tempVal = Sheets(source).Cells(lRow, "B").Value      'Assign the tempValue to compare
        tempValE = Sheets(source).Cells(lRow, "E").Value

        For tRow = 2 To LastTargetRow   'Loop through entire target sheet
        tempValT = Sheets(TARGET).Cells(tRow, "B").Value
            If (allSheets <> 2 Or allSheets <> 3) And tempVal = Sheets(TARGET).Cells(tRow, "B").Value And tempValE = Sheets(TARGET).Cells(tRow, "E").Value Then
            match = True
            ElseIf (allSheets <> 2 Or allSheets <> 3) And tempVal = Sheets(TARGET).Cells(tRow, "B").Value And tempValE = "" Then
            match = True
            ElseIf (allSheets <> 2 Or allSheets <> 3) And tempVal = Sheets(TARGET).Cells(tRow, "B").Value Then
            match = True
            'ElseIf Sheets(TARGET).Cells(tRow, "G").Value < DateAdd("m", -5, Date) Then
            'match = True
            End If
        Next tRow


        If match = False Then         'No Match found, copy row
            nRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
            For lCol = 1 To 26       'Copy entire row by looping through 6 columns
                Sheets("Sheet1").Cells(nRow, lCol).Value = Sheets(source).Cells(lRow, lCol).Value
            Next lCol
        End If

    Next lRow
    Next allSheets

End Sub

【问题讨论】:

  • 如果对您有帮助,请随时将我的答案标记为您的解决方案

标签: vba loops excel merge


【解决方案1】:

你有两个问题:

问题 1:您在 lRow 循环内重置 match = False,这必须在 tRow 循环内,否则如果第一个 match = True 命中则 match 永远不会重置

问题 2:无法输入 If match = False Then,因为它在您的 tRow 循环之外。所以match 设置在循环内,但If match = False Then 无法访问

所以工作代码应该是

Sub Button13() 'merge

    Dim lastSourceRow As Long, LastTargetRow As Long, allSheets As Long, lastSheet As Long
    Dim source As String, TARGET As Integer
    Dim tempVal As String, tempValE, tempValT
    Dim tRow As Long, lRow As Long, lCol As Long, nRow As Long
    Dim match As Boolean


    source = "Sheet2"
    lastSheet = "10"

    lastSourceRow = Sheets(source).Range("A" & Rows.Count).End(xlUp).Row


    For allSheets = 1 To lastSheet


    TARGET = allSheets
    LastTargetRow = Sheets(TARGET).Range("A" & Rows.Count).End(xlUp).Row

    For lRow = 2 To lastSourceRow     'Loop through Rows on currentsheet
        Count = "0"

        tempVal = Sheets(source).Cells(lRow, "B").Value      'Assign the tempValue to compare
        tempValE = Sheets(source).Cells(lRow, "E").Value

        For tRow = 2 To LastTargetRow   'Loop through entire target sheet
        tempValT = Sheets(TARGET).Cells(tRow, "B").Value
            If (allSheets <> 2 Or allSheets <> 3) And tempVal = Sheets(TARGET).Cells(tRow, "B").Value And tempValE = Sheets(TARGET).Cells(tRow, "E").Value Then
            match = True
            ElseIf (allSheets <> 2 Or allSheets <> 3) And tempVal = Sheets(TARGET).Cells(tRow, "B").Value And tempValE = "" Then
            match = True
            ElseIf (allSheets <> 2 Or allSheets <> 3) And tempVal = Sheets(TARGET).Cells(tRow, "B").Value Then
            match = True
            'ElseIf Sheets(TARGET).Cells(tRow, "G").Value < DateAdd("m", -5, Date) Then
            'match = True
            End If


        If match = False Then         'No Match found, copy row
            nRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
            For lCol = 1 To 26       'Copy entire row by looping through 6 columns
                Sheets("Sheet1").Cells(nRow, lCol).Value = Sheets(source).Cells(lRow, lCol).Value
            Next lCol
        End If

'2 moved lines
match = False                 'Reset boolean test for each new row
Next tRow

    Next lRow
    Next allSheets

End Sub

【讨论】:

  • 尚未检查问题出在哪里,但是当我使用测试变量运行宏时它没有返回任何值。
  • 您的来源看起来如何?我的测试变量都在工作
  • 我的代码超级混乱,但基本上它打印每个值 3 000 次(每次它循环通过 tRow 循环),无论它是否匹配?现在尝试调试.. 由于某种原因,循环永远不会将自己设置为 true。
猜你喜欢
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 2013-06-08
  • 2019-11-24
  • 2011-10-28
  • 2019-12-11
相关资源
最近更新 更多