I have the code below which deletes the blank rows in column A for all worksheets in the workbook - and it works well.
Code:
Option Explicit
Sub DeleteBlankRows()
Dim lRow As Long
Dim iCntr As Long
Dim ws As Worksheet
Dim wkbk1 As Workbook
Set wkbk1 = Workbooks("test.xlsm")
wkbk1.Activate
For Each ws In ThisWorkbook.Worksheets
' Find last row in column A
lRow = ws.Range("A" & ws.Rows.count).End(xlUp).Row
For iCntr = lRow To 1 Step -1
If IsEmpty(ws.Cells(iCntr, 1)) Or Trim(ws.Cells(iCntr, 1).Value) = "" Then
ws.Rows(iCntr).Delete
End If
Next iCntr
Next ws
End Sub
When I modify this code for a different purpose - to delete the blank rows in column B of a specific worksheet, then it just gets stuck in a loop and it did not delete a single row.
Option Explicit
Sub DeleteBlankRows()
Dim lRow As Long
Dim iCntr As Long
Dim ws As Worksheet
Dim wkbk1 As Workbook
Set wkbk1 = Workbooks("test.xlsm")
Set ws = wkbk1.Worksheets("sheet1")
wkbk1.Activate
ws.Activate
With ws
' Find last row in column A
lRow = ws.Range("B" & ws.Rows.count).End(xlUp).Row
For iCntr = lRow To 1 Step -1
If IsEmpty(ws.Cells(iCntr, 1)) Or Trim(ws.Cells(iCntr, 2).Value) = "" Then
ws.Rows(iCntr).Delete
End If
Next iCntr
End With
End Sub
I basically need help to let the code execute without getting stuck in a loop and to delete the blank rows found in column B on sheet1.
UPDATE:
I have uploaded a sample file to Google Drive if anyone would like to test on the file itself.
https://drive.google.com/file/d/1ImIqiA0znynSXAyZnUtpCG8mRIFlnXAl/view?usp=sharing