Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the actual usedrange

Tags:

I have a Excel worksheet that has a button.

When I call the usedRange() function, the range it returns includes the button part.

Is there anyway I can just get actual used range that contains data?

like image 966
thinkanotherone Avatar asked Sep 14 '11 21:09

thinkanotherone


People also ask

What does Activesheet UsedRange mean?

The UsedRange property represents the area described by the farthest upper-left and farthest lower-right nonempty cells in a worksheet and includes all cells in between.

What is UsedRange?

The UsedRange in VBA is a property of the worksheet 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 top-left used cell and last right used cells in a worksheet.

How do you create a UsedRange in Excel?

Select the Used RangePress Ctrl + Home, to select cell A1. Press Ctrl + Shift + End, to select all cells from A1 to the last used cell.

How do I select only the filled rows in Excel?

Or click on any cell in the column and then press Ctrl + Space. Select the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.


2 Answers

What sort of button, neither a Forms Control nor an ActiveX control should affect the used range.

It is a known problem that excel does not keep track of the used range very well. Any reference to the used range via VBA will reset the value to the current used range. So try running this sub procedure:

Sub ResetUsedRng()     Application.ActiveSheet.UsedRange  End Sub  

Failing that you may well have some formatting hanging round. Try clearing/deleting all the cells after your last row.

Regarding the above also see:

Excel Developer Tip

Another method to find the last used cell:

    Dim rLastCell As Range      Set rLastCell = ActiveSheet.Cells.Find(What:="*", After:=.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False) 

Change the search direction to find the first used cell.

like image 141
Reafidy Avatar answered Sep 24 '22 03:09

Reafidy


Readify made a very complete answer. Yet, I wanted to add the End statement, you can use:

Find the last used cell, before a blank in a Column:

Sub LastCellBeforeBlankInColumn() Range("A1").End(xldown).Select End Sub 

Find the very last used cell in a Column:

Sub LastCellInColumn() Range("A" & Rows.Count).End(xlup).Select End Sub 

Find the last cell, before a blank in a Row:

Sub LastCellBeforeBlankInRow() Range("A1").End(xlToRight).Select End Sub 

Find the very last used cell in a Row:

Sub LastCellInRow() Range("IV1").End(xlToLeft).Select End Sub 

See here for more information (and the explanation why xlCellTypeLastCell is not very reliable).

like image 22
JMax Avatar answered Sep 24 '22 03:09

JMax