2015-11-14 120 views
0

你好我試圖循環一個if-else所有列。但它返回1004錯誤不斷對於列的每個循環

Private Sub CommandButton1_Click() 

    Sheets("InputRAW").Columns(2).Copy Destination:=Sheets("OutputALL").Columns(1) 

    With ActiveSheet 
     Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 

    Dim rRng As Range 
    Set rRng = Range("A1:A" & Lastrow) 

    For Each cell In rRng.Cells 
     If IsEmpty(cell) Then 
      Worksheets("InputRAW").Range("C" & cell).Copy Destination:=Worksheets("OutputALL").Range("B" & cell) 
     Else 
      Worksheets("InputRAW").Range("A" & cell).Copy Destination:=Worksheets("OutputALL").Range("B" & cell) 
     End If 
    Next 

End Sub 
+2

'工作表( 「InputRAW」),範圍( 「C」 和cell.ROW ).Copy'(同目的地) – Jeeped

回答

0

你需要使用 「cell.Row」 而不是 「細胞」

For Each cell In rRng.Cells 
    If IsEmpty(cell) Then 
     Worksheets("InputRAW").Range("C" & cell.Row).Copy Destination:=Worksheets("OutputALL").Range("B" & cell.Row) 
    Else 
     Worksheets("InputRAW").Range("A" & cell.Row).Copy Destination:=Worksheets("OutputALL").Range("B" & cell.Row) 
    End If 
Next