+ Reply to Thread
Results 1 to 5 of 5

Delete all charts and empty rows from all sheets; won't change sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2014
    Location
    croatia
    MS-Off Ver
    Excel 2010
    Posts
    18

    Delete all charts and empty rows from all sheets; won't change sheets

    Hello,

    I am working on a macro that would:
    1. delete all charts from the sheet
    2. select rows from row 1 to last row that has data; this range will have some full and some empty rows
    3. delete empty rows from this range
    4. move onto the next Sheet and repeat
    5. do this for all sheets in a Workbook

    My Macro seems to be working more or less fine EXCEPT it would not do the "select rows, delete empty ones" on other sheets but the first one (one in focus). Any ideas what might be wrong?

    Thanks!

    Sub Delete_Charts_Rows_all_Sheets()
    Dim Ws As Worksheet, chtObj As ChartObject, i As Long, Lastrow As Long 
           
            
            With Application            
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
               
    
                    For Each Ws In ThisWorkbook.Worksheets
                               
                            For Each chtObj In Ws.ChartObjects
                                  chtObj.Delete
                            Next                                     
                                           
     
                              Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
                              Range("1:" & Lastrow).Select
                                       
                               
                            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
           .ScreenUpdating = True        
           End With
    
    End Su
    b

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,794

    Re: Delete all charts and empty rows from all sheets; won't change sheets

    Try changing

    Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
                              Range("1:" & Lastrow).Select
                                        
                            For i = Selection.Rows.Count To 1 Step -1
                                  If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
                                      Selection.Rows(i).EntireRow.Delete
    To

    Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = Lastrow To 1 Step -1
                 If WorksheetFunction.CountA(Rows(i)) = 0 Then
                            Rows(i).EntireRow.Delete

  3. #3
    Registered User
    Join Date
    05-08-2014
    Location
    croatia
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Delete all charts and empty rows from all sheets; won't change sheets

    Hey Alf,

    thanks for your imput, much appreciated! Unfortunately - no. The charts get deleted from all sheets but rows still get deleted only on the first (actually active sheet). I tried chahging

    LastRow = Active.Cells(Rows.Count, 1).End(xlUp).Row
    With

    LastRow = Ws.Cells(Rows.Count, 1).End(xlUp).Row
    but nope. It just wont work

  4. #4
    Registered User
    Join Date
    05-08-2014
    Location
    croatia
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Delete all charts and empty rows from all sheets; won't change sheets

    SOLVED!!! My god... after 2 days


    'FINAL - deletes all charts AND deletes empty rows from ALL sheets in workbook

    Sub Delete_Charts_and_EmtyRows()
    
    Dim Ws As Worksheet, chtObj As ChartObject, i As Long, LastRow As Long
            With Application                                        
                .Calculation = xlCalculationManual
                .ScreenUpdating = False
            
                        
                        For Each Ws In ThisWorkbook.Worksheets
                            Ws.Activate                             'KEY!! This seems to have solved looping through Sheets
                                
                                For Each chtObj In Ws.ChartObjects  'Finds chart objects and deletes them
                                        chtObj.Delete
                                Next
                                        
                                        
                                        LastRow = Ws.Cells(Rows.Count, 1).End(xlUp).Row     'Returns the number of last row with data, stores it in var LastRow
                                
                                
                                For i = LastRow To 1 Step -1        'Loops through the selection, deletes empty rows
                                    If WorksheetFunction.CountA(Rows(i)) = 0 Then
                                        Rows(i).EntireRow.Delete
                                    End If
                                Next
                                
                        
                        Next Ws
                    
                .Calculation = xlCalculationAutomatic
                .ScreenUpdating = True
            End With
    
    End Sub

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,794

    Re: Delete all charts and empty rows from all sheets; won't change sheets

    You solved it!! Good!!!

    Don't forget to mark your thread "Solved"

    Alf

    Ps
    To mark your thread solved do the following:
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. unable to delete entire row of empty cells in columns across multiple sheets.
    By CCLaMor in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-09-2014, 02:03 PM
  2. Removing Empty Rows On Multiple Sheets With Different Ranges
    By zebra4 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-02-2012, 05:40 PM
  3. Help deleting empty rows in all sheets
    By MZing81 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2012, 03:20 PM
  4. Delete Rows by Cell Value - Delete sheets w/out prompt
    By Jimmydageek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2010, 02:03 PM
  5. Avoiding empty rows when copying between sheets
    By dforte in forum Excel General
    Replies: 1
    Last Post: 08-06-2010, 07:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1