VBA code to delete rows in Excel

JustMyAlias

New Member
Joined
Jul 1, 2012
Messages
2
Seems like this would be super simple, but I'm not seeing my issue...
I have a spreadsheet that is locked. This is only bc the formulas have been mistakenly changed or incorrect by adding or deleting rows willy nilly.
I have created it with the max # of rows that should ever be needed, but typically they aren't all used.
After the spreadsheet is populated, I want VBA to remove the unused rows.
I will also do this for a second range (A35:E40).
Currently, only some of the rows are being deleted, instead of all blank rows in the range.
TIA!

This is the code:
VBA Code:
Sub DeleteExtraRows()

' DeleteExtraRows - to remove any extra/blank rows (10-26) on worksheet

    Dim strPassword As String
    Dim iRange As Range
            
    strPassword = "password"
    
    'remove password protection
    ActiveSheet.Unprotect password:=strPassword
    
    'User warning
    MsgBox "This will delete the blank rows. Are you sure you want to delete?", vbOKCancel
    
    'check cells A10:G26, if cells are empty, delete the row
        
    With ActiveSheet.Range("A10:G26")
    
        For Each iRange In .Rows
        
            If Application.CountA(iRange) = 0 Then
                
                iRange.EntireRow.Delete Shift:=xlUp
            
            End If
            
        Next

    End With

'add password protection
    ActiveSheet.Protect password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowInsertingRows:=True


End Sub
 

Attachments

  • spreadsheet.jpg
    spreadsheet.jpg
    218.3 KB · Views: 22

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Seems like this would be super simple, but I'm not seeing my issue...
I have a spreadsheet that is locked. This is only bc the formulas have been mistakenly changed or incorrect by adding or deleting rows willy nilly.
I have created it with the max # of rows that should ever be needed, but typically they aren't all used.
After the spreadsheet is populated, I want VBA to remove the unused rows.
I will also do this for a second range (A35:E40).
Currently, only some of the rows are being deleted, instead of all blank rows in the range.
TIA!

This is the code:
VBA Code:
Sub DeleteExtraRows()

' DeleteExtraRows - to remove any extra/blank rows (10-26) on worksheet

    Dim strPassword As String
    Dim iRange As Range
           
    strPassword = "password"
   
    'remove password protection
    ActiveSheet.Unprotect password:=strPassword
   
    'User warning
    MsgBox "This will delete the blank rows. Are you sure you want to delete?", vbOKCancel
   
    'check cells A10:G26, if cells are empty, delete the row
       
    With ActiveSheet.Range("A10:G26")
   
        For Each iRange In .Rows
       
            If Application.CountA(iRange) = 0 Then
               
                iRange.EntireRow.Delete Shift:=xlUp
           
            End If
           
        Next

    End With

'add password protection
    ActiveSheet.Protect password:=strPassword, DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowInsertingRows:=True


End Sub
Hi Just,
It looks like your code will only remove rows where the cells in the range = 0. Replace that 0 with ""

It also looks like you declared iRange, and are referencing it in your delete line, but you never defined the variable for iRange.
You need to remove
With ActiveSheet.Range("A10:G26")
and the accompanying End With
Replace it with
iRange = Range("A10:G26")

I think this should do it
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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