Select all rows up to last row that has data (xlUp command)

astrbac

Board Regular
Joined
Jan 22, 2015
Messages
55
Hi all,

I am writing a script that will:

- loop through all the sheets in the workbook
- delete all charts
- select all rows that have data (there will be some empty rows interspersed but this is ok)
- within this selection delete all empty rows

This is what I have assembled so far:

Code:
'Delete all chartObjects in ALL worksheets AND delete empty rows

Sub DeleteChartsAllSheets()
     Dim Ws As Worksheet, chtObj As ChartObject, i As Long
             
             With Application                                                 'turn off calculation and screen updating to speed up the macro.    
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
     
                    For Each Ws In ThisWorkbook.Worksheets       'Cycle through Sheets
            
                            For Each chtObj In Ws.ChartObjects        'Delete objects of type Chart
                                chtObj.Delete
                            Next

--------------------------------------------------------
HERE I'M HAVING PROBLEMS:
'Manually select rows from which I want to delete empty rows; 
NEED TO DO THIS AUTOMATICALLY
--------------------------------------------------------
                
                            For i = Selection.Rows.Count To 1 Step -1
                
                                If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
                                    Selection.Rows(i).EntireRow.Delete
                                End If
                        
                            Next
    
                    Next Ws
 
            .Calculation = xlCalculationAutomatic   'Turn screen updating and calculation back on
            .ScreenUpdating = True
            End With
        
End Sub

Many thanks, I'm learning from you guys by leaps and bounds!
Alex
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,216,746
Messages
6,132,478
Members
449,729
Latest member
davelevnt

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