1

I was trying to add an automatic macro to my very large workbook to at least trim some fat off by resetting the used range. I've tried numerous codes and cannot get it to work and can't figure out why. (It seemed to work a few time when I would manually delete every column to the right and bottom, then run the macro) I've tried the short ActiveSheet.UsedRange

Dim sht As Workshet Dim lng As Long For Each sht In ActiveWorkbook.Worksheets lng = sht.UsedRange.Rows.Count Next


Dim x As Long Dim y As Long x = ActiveSheet.UsedRange.Rows.Count y = ActiveSheet.UsedRange.Columns.Count

and a few other suggestions but it brings me some 30k cells below my data table each time.

Anyone know of simple surefire way of doing this? suggestions greatly appreciated as always, this WB is sitting around 70mb!

update Because I have tried numerous scripts to reset the used range without success unless I manually delete rows and columns, I have decided instead of trimming the use range to the last active row, I will simply delete the entire data table when the user saves (this has reduced the file size from about 70mb to 20mb!).

Many thanks

2
  • the only surefire way i have found is to manually delete all the rows / columns underneath and to right of the data... painful, but surefire. Jul 19, 2016 at 15:35
  • Tried Application.ScreenUpdating = False at the start and Application.ScreenUpdating = True at the end but it still changes to the data tab, am I just confused as to the function of screenupdating?
    – Awill
    Jul 21, 2016 at 14:24

3 Answers 3

1

This works for me, but it takes about 20 seconds to run:

Sub resetter()
    Dim N As Long, i As Long

    With Application.WorksheetFunction

        N = Rows.Count
        For i = N To 1 Step -1
            If .CountA(Cells(i, 1).EntireRow) <> 0 Then Exit For
        Next i
        Range(Cells(N, 1), Cells(i + 1, 1)).EntireRow.Delete

        N = Columns.Count
        For i = N To 1 Step -1
            If .CountA(Cells(1, i).EntireColumn) <> 0 Then Exit For
        Next i
        Range(Cells(1, N), Cells(1, i + 1)).EntireColumn.Delete

    End With
End Sub
4
  • results were the same. The codes work if I test them in a new sheet by doing the whole enter data in a cell a few lines down then jump to the last row, enter data and delete it to activate the range. I test run the macro and my used range is back up to line x where it should be. It just seems nothing works on this sheet, and sometimes not even the manual row deleting, I wonder if it has anything to do with it being a data table? For a little extra detail without uploading it, I've a data table with a few hundred K rows, I then remove unneeded rows and shift all the data up.
    – Awill
    Jul 19, 2016 at 20:04
  • so tried again this morning by deleting all of the data from my table and running this. It did not reset the used range but it did however delete all but one row from my table, when I ran my script right after, it actually reset. Now I guess to figure out a way to remove all unused rows and columns that doesn't take 10-15 seconds and follow it up with UsedRange
    – Awill
    Jul 20, 2016 at 12:33
  • @Awill We can speed it up greatly if the first totally empty row or column defines the UsedRange boundary. That way we do not have to examine every row from the bottom upwards. Jul 20, 2016 at 12:39
  • the defined row would then need to remain constant am I right? I'm wondering if it'll just be much easier and save a massive amount of space to just delete the entire data table and reset the used range after refreshing the pivot tables or upon closing the spreadsheet. And when I say massive I mean more than a couple hundred thousand rows which vary month to month.
    – Awill
    Jul 20, 2016 at 16:39
0
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
For Each Sh In ThisWorkbook.Worksheets
x = Sh.UsedRange.Rows.Count
Next Sh
End Sub

I found this somewhere online (sorry for not remembering), and it worked for me

1
0

see the update to my question for explanation, decided on this as the best solution. `

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim lng As Long, sht As Worksheet

    Application.ScreenUpdating = False
    On Error Resume Next
         Application.DisplayAlerts = False
       Sheets("data").Range("denialstable1").Delete
       Application.DisplayAlerts = True

        For Each sht In ActiveWorkbook.Worksheets
            lng = sht.UsedRange.Rows.Count
      Next
      Application.ScreenUpdating = True
    End Sub`
1
  • how exactly does this count resetting the used range?
    – pbou
    Jan 25, 2019 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.