Home » excel » excel – Why my code is not working? Is it because of LastRow0 = .Cells(.Rows.Count, "AI").End(xlUp).row

excel – Why my code is not working? Is it because of LastRow0 = .Cells(.Rows.Count, "AI").End(xlUp).row

Posted by: admin May 14, 2020 Leave a comment

Questions:

The below code is working only if my table finishes on column AJ. My table can finish before column AJ. How I can amend the code so it works when my table finishes in any column?

Thank you in advance.

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
How to&Answers:

You have found the lastRow, now you need the 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