Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel resetting "UsedRange"

Tags:

excel

vba

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?

like image 821
Claus Avatar asked Mar 02 '16 15:03

Claus


People also ask

What is UsedRange Excel?

A used range includes any cell that has ever been used. For example, if cell A1 contains a value, and then you delete the value, then cell A1 is considered used. In this case, the UsedRange property will return a range that includes cell A1.

What does UsedRange mean in VBA?

The UsedRange in VBA is a worksheet property that returns a range object representing the range used (all Excel cells used or filled in a worksheet) on a particular worksheet. It is a property representing the area covered or bounded by the top-left used cell and the last right-used cells in a worksheet.

How do you reset the bottom row in Excel?

Press Ctrl + Home and then Ctrl + End (This should take you the correct last cell in the used range (above blank rows). Click Save again.


1 Answers

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

like image 164
Harley Avatar answered Nov 09 '22 18:11

Harley