Deleting Rows in VBA

markjarvisltd

Board Regular
Joined
Sep 17, 2004
Messages
92
I have the current code but it keeos failing at the delete row line
Can anyone explain why?

Private Sub CommandButton1_Click()

Dim iRow As Integer
Dim iRowsProcessed As Integer
Dim bAnotherBlank As Boolean

iRowsProcessed = 0

Application.ScreenUpdating = False
Application.Calculation = xlManual

For iRow = 1 To 200
bAnotherBlank = True

If Cells(iRow, 1).Value = "" Then
iRow.EntireRow.Delete

' Have we just moved another blank up to replace
' the one we deleted?
While (bAnotherBlank = True) And (iRowsProcessed < 200)
If Cells(iRow, 1).Value = "" Then
iRow.EntireRow.Delete
Else
bAnotherBlank = False
End If
iRowsProcessed = iRowsProcessed + 1
Wend
Else
iRowsProcessed = iRowsProcessed + 1
End If
Next iRow

Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
mark

Failing it what way?

When deleting you should start from the bottom and work up.
Code:
For iRow = 200 To 1 Step -1
 
Upvote 0
Try

Code:
If Cells(iRow, 1).Value = "" Then
Cells(iRow, 1).EntireRow.Delete

and you should loop backwards as Norie said.
 
Upvote 0
Ive corrected as advised now nothing happens

Private Sub CommandButton1_Click()

Dim iRow As Integer
Dim iRowsProcessed As Integer
Dim bAnotherBlank As Boolean

iRowsProcessed = 0

Application.ScreenUpdating = False
Application.Calculation = xlManual

For iRow = 200 To 1 Step -1
bAnotherBlank = True

If Cells(iRow, 1).Value = "" Then
Cells(iRow, 1).EntireRow.Delete

' Have we just moved another blank up to replace
' the one we deleted?
While (bAnotherBlank = True) And (iRowsProcessed < 200)
If Cells(iRow, 1).Value = "" Then
Cells(iRow, 1).EntireRow.Delete
Else
bAnotherBlank = False
End If
iRowsProcessed = iRowsProcessed + 1
Wend
Else
iRowsProcessed = iRowsProcessed + 1
End If
Next iRow

Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Try this

Code:
Private Sub CommandButton1_Click()

Dim iRow As Integer
Application.ScreenUpdating = False
Application.Calculation = xlManual

For iRow = 200 To 1 Step -1
    If Cells(iRow, 1).Value = "" Then Cells(iRow, 1).EntireRow.Delete
Next iRow

Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks thats neat
Ive discovered a problem. In posting in from a web page the apparently blank cells are not. If I clear cell your code works.
Is there an way within the code to clear these apparently not so blank cells?
 
Upvote 0
Try

Code:
Private Sub CommandButton1_Click()

Dim iRow As Integer
Application.ScreenUpdating = False
Application.Calculation = xlManual

For iRow = 200 To 1 Step -1
    If WorksheetFunction.Clean(Cells(iRow, 1).Value) = "" Then Cells(iRow, 1).EntireRow.Delete
Next iRow

Application.Calculation = xlAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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