8
votes

Don't know what I'm missing, but the examples I see posted do not appear to work.

I import data from a web query. I set the query to clear unused cells when it re-queries. As shown here in the last radio button

I used this imported data to generate a report of variable length.

However if the user (as they need to do in my case) insert rows then the ActiveSheet.UsedRange is expanded. This means I cannot any longer just do a "Ctrl-End" to find the last row in the data set when a new query is performed.

I can easily clear any data with ActiveSheet.UsedRange.Clear. However if the previous query generated a 2 or 3 page report any subsequent query will also be that long even when there is less data because the "UsedRange" still points to that last row way down there.

The examples shown like

ActiveSheet.UsedRange
ActiveSheet.UsedRange.Clear
a = ActiveSheet.UsedRange.Rows.Count

do not reset the range.

MS defines UsedRange as a readOnly property.

It appears what needs to happen is a "File Save" in order to complete the action.

ActiveWorkbook.Save

One post noted that in older versions of Excel you also had to close the workbook and reopen it to complete the action.

I would like to know 1. What is the version cutoff where this behavior changed? 2. Is there some other method using a VBA macro which will reset the range?

13
I forgot to add that to reset the range, first you select "UsedRange" then delete the content "Selection.Delete Shift:=xlUp" before executing the save - Claus
From Memory you just need to select cell 1,1 in any relevant sheet you want to reset then save - sirplus
You state that "This means I cannot any longer just do a "Ctrl-End" to find the last row in the data set when a new query is performed." If this is indeed your problem then you can just find the last row of data by using a VBA command such as "lastRow = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row". This would not depend on the size of the UsedRange, which you may want to reset for other reasons (to reduce the size of the sheet, for example, but you do not indicate that as your goal). - rxex

13 Answers

6
votes

I only needed to use Worksheets("Sheet1").UsedRange.Calculate after deleting rows to reset the range.

4
votes

Best code that worked for me:

Sub DeleteUnused()
Dim myLastRow As Long
Dim myLastCol As Long
Dim dummyRng As Range
Dim AnyMerged As Variant
'http://www.contextures.on.ca/xlfaqApp.html#Unused
'Helps to reset the usedrange by deleting rows and columns AFTER your true used range

    'Check for merged cells
    AnyMerged = ActiveSheet.UsedRange.MergeCells
    If AnyMerged = True Or IsNull(AnyMerged) Then
        MsgBox "There are merged cells on this sheet." & vbCrLf & _
               "The macro will not work with merged cells.", vbOKOnly + vbCritical, "Macro will be Stopped"
        Exit Sub
    End If

    With ActiveSheet
        myLastRow = 0
        myLastCol = 0
        Set dummyRng = .UsedRange
        On Error Resume Next
        myLastRow = _
        .Cells.Find("*", after:=.Cells(1), _
                    LookIn:=xlFormulas, lookat:=xlWhole, _
                    searchdirection:=xlPrevious, _
                    searchorder:=xlByRows).Row
        myLastCol = _
        .Cells.Find("*", after:=.Cells(1), _
                    LookIn:=xlFormulas, lookat:=xlWhole, _
                    searchdirection:=xlPrevious, _
                    searchorder:=xlByColumns).Column
        On Error GoTo 0

        If myLastRow * myLastCol = 0 Then
            .Columns.Delete
        Else
            .Range(.Cells(myLastRow + 1, 1), _
                   .Cells(.Rows.Count, 1)).EntireRow.Delete
            .Range(.Cells(1, myLastCol + 1), _
                   .Cells(1, .Columns.Count)).EntireColumn.Delete
        End If
    End With

End Sub
1
votes

I've used Jeeped solution and worked for me when i add .Activate, so:

With Worksheets("Sheet1")
        Debug.Print .UsedRange.Address(0, 0)
        .UsedRange.Clear
        .UsedRange   
        .Activate
        Debug.Print .UsedRange.Address(0, 0)
  End With

I'm using Excel2013

1
votes
  1. select cell 1,1 in any sheets you want to reset the UsedRange property
  2. Calculate all worksheets in all open workbooks, regardless of whether they changed since last calculation (To Calculate Fully Ctrl+Alt+F9)
  3. Save the workbook

Works for me on all versions of excel

0
votes

If you call the Worksheet.UsedRange property by itself, it will reset.

    With Worksheets("Sheet1")
        Debug.Print .UsedRange.Address(0, 0)
        .UsedRange.Clear
        .UsedRange    '<~~ called by itself will reset it
        Debug.Print .UsedRange.Address(0, 0)
    End With

This extra step is unnecessary in xl2010 and above with all appropriate service packs installed.

0
votes

Here is how I inserted your code.

    Sheets("Edit Data").Select
'    Range("A6").Select
'    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
'    Selection.Delete Shift:=xlUp
'    ActiveWorkbook.Save
    With Worksheets("Edit Data")
        Debug.Print .UsedRange.Address(0, 0)
        .UsedRange.Clear
        .UsedRange    '<~~ called by itself will reset it
        Debug.Print .UsedRange.Address(0, 0)
    End With

Here is the full used range with data Ctrl-End shows usedRange

Here is the used range after your code executed enter image description here

The end of range should be i7 instead it is still i26

However the code which I commented out does reset range to i7

From what you are saying just to confirm. My commented out code will only work for Excel 2010 and newer. We have some 2007 versions hanging around. For those the workbook will actually have to be closed and reopened for the range to reset?

Note- the code examples were executed on version 2016

0
votes

This may or may not suit your data needs, but if your data is all in one contiguous block, you can use CurrentRegion instead of UsedRange, like this:

With Cells(1, 1).CurrentRegion
 MsgBox "I have " & .Rows.Count & " rows and " & .Columns.Count & " columns of data."
End With

Of course, if the region you care about does not start at cell A1, or if your sheet contains multiple contiguous regions that you care about, this option will not work. Depending on how predictable your data is, you can usually find at least one cell in each block of data, and once you have that, CurrentRegion will give you the range of the entire block.

0
votes

Thanks to Claus for having the correct answer, but it is incomplete, and he completed it with a comment on the main post. I'll answer here to combine the useful bits into a single working solution.

Note: I have not tried variations to see which steps are necessary, but these steps work to clear unnecessary used range.

Sub ResetUsedRange()
dim rngToDelete as range

set rngToDelete = 'Whatever you want

rngToDelete.EntireRow.Clear
rngToDelete.EntireRow.Select
Selection.Delete Shift:=xlUp

ActiveWorkbook.Save 'This step has been stated to be necessary for the range to reset.

End Sub
0
votes

This is the solution I used.

Sub CorrectUsedRange()
    Dim values
    Dim usedRangeAddress As String
    Dim r As Range
    'Get UsedRange Address prior to deleting Range
    usedRangeAddress = ActiveSheet.UsedRange.Address
    'Store values of cells to array.
    values = ActiveSheet.UsedRange
    'Delete all cells in the sheet
    ActiveSheet.Cells.Delete
    'Restore values to their initial locations
    Range(usedRangeAddress) = values
End Sub
0
votes

This is what ended up working for me. I feel there has to be a better way but no others worked for me.

Sub ClearRangeData()
Dim S1 As Worksheet

Set S1 = Sheets("Your Sheet Name")     'Define what sheets we are using

'------- Remove all the old data -----
S1.Activate
With ActiveSheet
    S1_rows = S1.UsedRange.Rows.Count
    For I = S1_rows To 1 Step -1
        Cells(I, 1).EntireRow.Delete
    Next
End With
End Sub
-1
votes

This worked for me:

Worksheets("Sheet1").UsedRange.Clear
Worksheets("Sheet1").UsedRange = ""

It appears that inserting a value into the UsedRange resets it. After this action I can go

MyCurrentRow = Worksheets("Sheet1").Range("A:A").SpecialCells(xlCellTypeLastCell).Row

MyCurrentRow comes now back as 1, and I can just count from there. When I did not assign a value into UsedRange, that LastCell value did not reset. No Save required.

-2
votes

This works for me in Excel 2010:

Worksheets("Sheet1").UsedRange.Clear  
Worksheets("Sheet1").UsedRange.Calculate
-4
votes

I double checked to make sure all the latests patches and service packs have been installed and they were.

I'm running Windows 10 and Excel 2016 version 16.0.6568.2034

I found that the range would only reset with the

ActiveSheet.UsedRange.Clear

And most importantly

ActiveWorkbook.Save

without the save command the range is not reset