为什么我的代码无法正常工作?是因为LastRow0 = .Cells(.Rows.Count,“ AI”)。End(xlUp).row

时间:2019-03-25 20:12:32

标签: excel vba

仅当我的表在AJ列上完成时,以下代码才有效。我的表可以在AJ列之前完成。我该如何修改代码,以便当我的表在任何列中完成时可以工作?

谢谢。

Dim row As Long, Column As Long
With ThisWorkbook.Worksheets("Data")
    LastRow0 = .Cells(.Rows.Count, "AI").End(xlUp).row
    For row = 8 To LastRow0 Step 3
        For Column = 5 To 35
            If Cells(row, Column).Value = "" Then
                Cells(row, Column).Value = 0
            End If
        Next Column
    Next row
End With

1 个答案:

答案 0 :(得分:2)

您已经找到了lastRow,现在需要lastColumn。

Dim row As Long, Column As Long
With ThisWorkbook.Worksheets("Data")
    LastRow0 = .Cells(.Rows.Count, "AI").End(xlUp).row
    .UsedRange
    LastCol0 = .UsedRange.Columns(.UsedRange.Columns.Count).Column
    For row = 8 To LastRow0 Step 3
        For Column = 5 To LastCol0
            If Cells(row, Column).Value = "" Then
                Cells(row, Column).Value = 0
            End If
        Next Column
    Next row
End With