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.
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?
Here is how I inserted your code.
Here is the full used range with data
Here is the used range after your code executed
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
Best code that worked for me:
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.
This works for me in Excel 2010:
This is the solution I used.