2017-10-04 45 views
0

如果AG_Label = M_Label,代碼將運行並粘貼,但僅限於第一個實例。 我的循環有問題,但我對VBA很新,所以我不知道如何解決它。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 
+0

對於初學者來說,解決這個線 - 'AROW =表(「公司」)範圍(「A」和Rows.Count).END (xlUp).Row'有一個像這樣的行的限定引用 - 'ARow = Sheets(「AG」)。Range(「A」&Sheets(「AG」).Rows.Count).End(xlUp).Row' – braX

+0

您的'Count = Count + 1'緊挨在'End Sub'看起來可疑之前 - 絕對沒有任何一點遞增過程級變量,然後通過退出p來丟棄它rocedure。這應該在你的循環裏面嗎? – YowE3K

+1

註釋掉'On Error Resume Next',除非有很好的理由。 –

回答

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