简体   繁体   中英

Delete Empty rows in multiple sheets

I wrote the code below to delete empty rows in multiple sheets. It works fine for few sheets, but it takes quite some time and I had to run it few times to get the work done. Now I need to apply this to about 100 sheets. Can someone help me to modify this code for that.

Sub delete_empty_rows()

Dim ws As Worksheet
Dim x As Integer
Dim y As String

For Each ws In ThisWorkbook.Sheets
  For x = 12 To 60
    y = Cells(x, 3).Value

    If y = "" Then
      ws.Rows(x).Delete
    End If
  Next x
Next ws
End Sub

The bug in the code is that it misses a worksheet reference in the line y = Cells.(x, 3).Value .

It should be y = ws.Cells.(x, 3).Value .

Without the ws , the check is done towards the ActiveSheet and the actions are taken towards the looped sheet.


There are many ways to do what you need, this is one of them, deleting rows with a number in column A, between range("A1:A10"). If you loop through all the worksheets, For Each wks in ThisWorkbook.Worksheets the code will do what you need with some changes:

Sub RemoveNumbersFromColumnA()

    Dim i As Long
    Dim myCell As Range
    Dim rowsToDelete As Range

    For i = 1 To 10
        Set myCell = Worksheets(1).Cells(i, "A")
        If IsNumeric(Left(myCell, 1)) Then
            Debug.Print myCell; " adding..."
            If rowsToDelete Is Nothing Then
                Set rowsToDelete = myCell.EntireRow
            Else
                Set rowsToDelete = Union(rowsToDelete, myCell.EntireRow)
            End If
        End If
    Next i

    If Not rowsToDelete Is Nothing Then
        rowsToDelete.Delete
    End If

End Sub

From this answer - https://stackoverflow.com/a/59637372/5448626

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