简体   繁体   中英

VBA Excel Loop All Sheets

My code below only works on sheet1 and doesn't loop through all worksheets.

Would anyone know why, please?

Sub DeleteValueRows()

    Dim Firstrow As Long
    Dim Lastrow2 As Long
    Dim Lrow As Long
    Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

        Firstrow = 1
        Lastrow2 = Cells(Rows.Count, "A").End(xlUp).Row
        For Lrow = Lastrow2 To Firstrow Step -1

            With Cells(Lrow, "A")
                If Not IsError(.Value) Then
                    If .Value = "0" Then .Value = ""

                End If
            End With
        Next Lrow
Next ws

End Sub

With feat. the 'Dot Issue'

Sub DeleteValueRows()

    Dim Firstrow As Long
    Dim Lastrow2 As Long
    Dim Lrow As Long
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets

        With ws

            Firstrow = 1
            Lastrow2 = .Cells(.Rows.Count, "A").End(xlUp).Row

            For Lrow = Lastrow2 To Firstrow Step -1

                With .Cells(Lrow, "A")
                    If Not IsError(.Value) Then
                        If .Value = "0" Then .Value = ""

                    End If
                End With

            Next Lrow

        End With

    Next ws

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM