Delete cells and move up 2003

kingdomb

New Member
Joined
May 17, 2011
Messages
16
I'm trying to delete just A:_ & B:_ and move up if B:_ is empty. This works for the first sheet but then the second sheet stalls and I eventually have to shut down the program. Here is what I have so far.

Sub SStep2()
Sheets("1st").Select
Dim I As Long
Dim FIRSTROW As Long
Dim LASTROW As Long
Application.ScreenUpdating = False
FIRSTROW = ActiveSheet.UsedRange.Rows.Row
LASTROW = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows.Row - 1
For I = LASTROW To FIRSTROW Step -1
If (Cells(I, "B") = "") Then
Range(Cells(I, "A"), Cells(I, "B")).Delete Shift:=xlUp
End If
Next I
Application.ScreenUpdating = True

Sheets("2nd").Select
Application.ScreenUpdating = False
FIRSTROW = ActiveSheet.UsedRange.Rows.Row
LASTROW = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows.Row - 1
For I = LASTROW To FIRSTROW Step -1
If (Cells(I, "B") = "") Then
Range(Cells(I, "A"), Cells(I, "B")).Delete Shift:=xlUp
End If
Next I
Application.ScreenUpdating = True

Sheets("3rd").Select
Application.ScreenUpdating = False
FIRSTROW = ActiveSheet.UsedRange.Rows.Row
LASTROW = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows.Row - 1
For I = LASTROW To FIRSTROW Step -1
If (Cells(I, "B") = "") Then
Range(Cells(I, "A"), Cells(I, "B")).Delete Shift:=xlUp
End If
Next I
Application.ScreenUpdating = True

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You might check the extents of the used range on the second sheet and make sure you haven't applied formatting or down something else that causes it to include all the rows in sheet.

Then try this:

Code:
Sub SStep2()
    Dim wks       As Worksheet
    Dim r         As Range
    Dim iRow      As Long
 
    Application.ScreenUpdating = False
 
    For Each wks In Worksheets(Array("1st", "2nd", "3rd"))
        Set r = Intersect(wks.UsedRange.EntireRow, wks.Columns("A:B"))
 
        If Not r Is Nothing Then
            For iRow = r.Rows.Count To 1 Step -1
                If IsEmpty(r(iRow, "B").Value) Then
                    r.Rows(iRow).Delete Shift:=xlUp
                End If
            Next iRow
        End If
    Next wks
 
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
You're correct! I had some sheets with endless columns and rows. I changed the formula like this though.

Dim I As Long
Dim FIRSTROW As Long
Dim LASTROW As Long
Application.ScreenUpdating = False
FIRSTROW = 7
LASTROW = 100
For I = LASTROW To FIRSTROW Step -1
If (Cells(I, "B") = "") Then
Range(Cells(I, "A"), Cells(I, "B")).Delete Shift:=xlUp
End If
Next I


Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,345
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top