VBA循环错误。只粘贴一个结果

时间:2017-10-04 19:01:46

标签: arrays string vba loops copy-paste

如果AG_Label = M_Label,则代码运行并粘贴,但仅适用于第一个实例。 我的循环有问题,但我对VBA很新,所以我不知道如何解决它。

    Dim mrow As Long, ARow As Long, iRows As Long, srow As String
Dim x As Long, j As Long, i As Long
Dim AG_Label As String, AG_val As String, SNL_val As String, SNL_Label As String, M_Label As String, Rng As Range
Dim Vval As Long, Speriod As String, aperiod As String
Dim Count As Integer, Ajay As Variant

Count = 2

ARow = Sheets("AG").Range("A" & Rows.Count).End(xlUp).Row
Do
For i = 2 To ARow
    AG_Label = Sheets("AG").Cells(i, "N").Value
    'mrow = Sheets("Mappings").Range("A" & Rows.count).End(xlUp).Row
    'For j = 2 To mrow
        'M_Match_1 = Sheets("Mappings").Cells(j, "P").Value
        M_Label = Sheets("Mappings").Cells(Count, "C").Value


        If AG_Label = M_Label And Sheets("Mappings").Cells(Count, "L").Value = "FRY-9C" Then
            Sheets("Mappings").Cells(Count, "J").copy
            Sheets("AG").Cells(i, "AM").PasteSpecial Paste:=xlPasteValues, _
              Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                On Error Resume Next
        End If

Application.CutCopyMode = False

Next
'i = i + 1

Loop Until i = ARow + 1

Count = Count + 1
End Sub

1 个答案:

答案 0 :(得分:0)

这可能不是您想要的,但它应该让您更好地了解如何在循环中构建循环:

    Dim mrow As Long, ARow As Long, iRows As Long, srow As String
    Dim x As Long, j As Long, i As Long
    Dim AG_Label As String, AG_val As String, SNL_val As String, SNL_Label As String, M_Label As String, Rng As Range
    Dim Vval As Long, Speriod As String, aperiod As String
    Dim Count As Long, Ajay As Variant

    ARow = Sheets("AG").Range("A" & Sheets("AG").Rows.Count).End(xlUp).Row
    mrow = Sheets("Mappings").Range("A" & Sheets("Mappings").Rows.count).End(xlUp).Row
    For i = 2 To ARow
        On Error Resume Next
        AG_Label = Sheets("AG").Cells(i, "N").Value
        On Error GoTo 0
        For Count = 2 To mrow
            'M_Match_1 = Sheets("Mappings").Cells(j, "P").Value
            M_Label = Sheets("Mappings").Cells(Count, "C").Value

            If AG_Label = M_Label And Sheets("Mappings").Cells(Count, "L").Value = "FRY-9C" Then
                Sheets("Mappings").Cells(Count, "J").copy
                Sheets("AG").Cells(i, "AM").PasteSpecial Paste:=xlPasteValues, _
                   Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If    
        Next
    Next
    Application.CutCopyMode = False
End Sub

注意:我认为在变量AG_Label的设置周围使用错误屏蔽是一个坏的,坏的想法。 (这意味着你将通过循环执行多次,使用相同的AG_Label值,直到你遇到从N列获得一个新的有效值的情况。)下面是一些停止处理的替代代码像#N/A这样的事情,我怀疑你可能想要On Error

    Dim mrow As Long, ARow As Long, iRows As Long, srow As String
    Dim x As Long, j As Long, i As Long
    Dim AG_Label As String, AG_val As String, SNL_val As String, SNL_Label As String, M_Label As String, Rng As Range
    Dim Vval As Long, Speriod As String, aperiod As String
    Dim Count As Long, Ajay As Variant

    ARow = Sheets("AG").Range("A" & Sheets("AG").Rows.Count).End(xlUp).Row
    mrow = Sheets("Mappings").Range("A" & Sheets("Mappings").Rows.count).End(xlUp).Row
    For i = 2 To ARow
        If Not IsError(Sheets("AG").Cells(i, "N").Value) Then
            AG_Label = Sheets("AG").Cells(i, "N").Value
            For Count = 2 To mrow
                'M_Match_1 = Sheets("Mappings").Cells(j, "P").Value
                M_Label = Sheets("Mappings").Cells(Count, "C").Value

                If AG_Label = M_Label And Sheets("Mappings").Cells(Count, "L").Value = "FRY-9C" Then
                    Sheets("Mappings").Cells(Count, "J").copy
                    Sheets("AG").Cells(i, "AM").PasteSpecial Paste:=xlPasteValues, _
                       Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                End If    
            Next
        End If
    Next
    Application.CutCopyMode = False
End Sub