对于列的每个循环

时间:2015-11-14 13:21:21

标签: excel vba excel-vba

您好我试图为所有列循环一个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

1 个答案:

答案 0 :(得分:0)

你需要使用" cell.Row"而不是" cell"

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