Vba 如何使for循环跨图纸合并新值?

Vba 如何使for循环跨图纸合并新值?,vba,loops,excel,merge,Vba,Loops,Excel,Merge,我有人帮我做了这个初始代码,我试图修改它,但它是错误的 我需要将电子表格中的表2与表4至表10进行比较,如果行e或b的值与任何其他行不匹配。将整行复制到第1页的底部 这是我到目前为止得到的结果,但该值没有设置为true,它会在每张纸之后打印。我是木棍 Sub Button13() 'merge Dim lastSourceRow As Long, LastTargetRow As Long, allSheets As Long, lastSheet As Long Dim s

我有人帮我做了这个初始代码,我试图修改它,但它是错误的

我需要将电子表格中的表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
子按钮13()'合并
调暗lastSourceRow为长、LastTargetRow为长、所有图纸为长、lastSheet为长
将源设置为字符串,目标设置为整数
将tempVal变暗为字符串,tempValE,tempValT
暗管长度、低管长度、低管长度、低管长度、低管长度
作为布尔值的Dim匹配
source=“Sheet2”
lastpheet=“10”
lastSourceRow=Sheets(source).Range(“A”&Rows.Count).End(xlUp).Row
对于所有图纸=1到最后一张图纸
目标=所有工作表
LastTargetRow=工作表(目标).Range(“A”和Rows.Count).End(xlUp).Row
对于lRow=2到lastSourceRow'循环通过currentsheet上的行
Count=“0”
match=False“为每一新行重置布尔测试
tempVal=工作表(源)。单元格(lRow,“B”)。值“指定要比较的tempValue”
tempValE=图纸(源).Cells(lRow,“E”).值
对于tRow=2到LastTargetRow,在整个目标工作表中循环
tempValT=板材(目标).Cells(tRow,“B”).值
如果(所有表2或所有表3)和tempVal=表(目标).Cells(tRow,“B”).值和tempValE=表(目标).Cells(tRow,“E”).值,则
匹配=真
ElseIf(所有表2或所有表3)和tempVal=Sheets(TARGET).Cells(tRow,“B”)。Value和tempValE=”“然后
匹配=真
ElseIf(所有表2或所有表3)和tempVal=表(目标)。单元格(tRow,“B”)。然后计算值
匹配=真
'ElseIf Sheets(TARGET).Cells(tRow,“G”).Value
您有两个问题:

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

问题2:
如果match=False,则无法输入
,因为它在您的
tRow
循环之外。因此,
match
在循环内设置,但如果match=False则不能通过
访问

所以工作代码应该是

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
子按钮13()'合并
调暗lastSourceRow为长、LastTargetRow为长、所有图纸为长、lastSheet为长
将源设置为字符串,目标设置为整数
将tempVal变暗为字符串,tempValE,tempValT
暗管长度、低管长度、低管长度、低管长度、低管长度
作为布尔值的Dim匹配
source=“Sheet2”
lastpheet=“10”
lastSourceRow=Sheets(source).Range(“A”&Rows.Count).End(xlUp).Row
对于所有图纸=1到最后一张图纸
目标=所有工作表
LastTargetRow=工作表(目标).Range(“A”和Rows.Count).End(xlUp).Row
对于lRow=2到lastSourceRow'循环通过currentsheet上的行
Count=“0”
tempVal=工作表(源)。单元格(lRow,“B”)。值“指定要比较的tempValue”
tempValE=图纸(源).Cells(lRow,“E”).值
对于tRow=2到LastTargetRow,在整个目标工作表中循环
tempValT=板材(目标).Cells(tRow,“B”).值
如果(所有表2或所有表3)和tempVal=表(目标).Cells(tRow,“B”).值和tempValE=表(目标).Cells(tRow,“E”).值,则
匹配=真
ElseIf(所有表2或所有表3)和tempVal=Sheets(TARGET).Cells(tRow,“B”)。Value和tempValE=”“然后
匹配=真
ElseIf(所有表2或所有表3)和tempVal=表(目标)。单元格(tRow,“B”)。然后计算值
匹配=真
'ElseIf Sheets(TARGET).Cells(tRow,“G”).Value
尚未检查问题所在,但当我使用测试变量运行宏时,它不会返回任何值。您的源代码看起来如何?我的测试变量都在工作,我的代码非常混乱,但基本上它会打印每个值,比如3000次(每次我在tRow循环中循环),不管它是否匹配?正在尝试调试。。由于某些原因,循环永远不会自动设置为真。如果我的答案对您有帮助,请随时将其标记为您的解决方案