What is the last cell in a worksheet ?

sean_f_howard

New Member
Joined
Dec 9, 2004
Messages
29
Why is it that
Code:
ActiveSheet.SpecialCells(xlCellTypeLastCell))

sometimes gives a different range from
Code:
ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell))

The issue arose by setting the print range for a worksheet. The first solution worked OK, but usually included an extra (and indeed empty) column from the far right of the worksheet.
The solution works great for all cases but I want to know if anybody can tell me why this is so.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
For me your first code statement produced an error. It should be:

Code:
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell)

The UsedRange expands as you enter more data on a worksheet. But it does not contract as data is removed - you need to save the workbook to update it. So the first statement can return an unexpected result.

Accessing the UsedRange property actually updates the range (like saving the workbook), so SpecialCells returns the expected result.
 
Upvote 0
Thanks for that Andrew. It kind of makes sense and I shall simply remember to use the two combined whenever possible.

On the same theme sometimes I have a worksheet whose last cell is way beyond the actual cells used in the worksheet. I have tried clearing,deleting column/rows, saving etc. but all to no avail. The ONLY thing that seems to reset the last cell to it's correct address is copying the worksheet, then the copy is right (but the original still wrong)
Am I missing a simple solution to this. I do not want to always copy entire worksheets as it seems a bit silly
 
Upvote 0
SpecialCells LastCell, UsedRange and CurrentRegion are iffy and give bad results depending on the version, when the workbook was saved, formats, and if breaks occur in the data. This is the most reliable way I know, as long as you are starting from cell A1 (modify if not).

Sub Test1()
Dim LR As Long, LC As Integer, MyRange As Range
LR = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
LC = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set MyRange = Range(Cells(1, 1), Cells(LR, LC))
MyRange.Select 'or Copy or whatever
End Sub
 
Upvote 0
I seem to recall that in older versions of Excel (I have Excel 2000) referencing a cell beyond the UsedRange in a formula expanded the UsedRange. But I haven't experienced that recently.

Maybe just accessing the UsedRange in code would solve your problem:

Code:
MsgBox ActiveSheet.UsedRange.Address

Also look here:

http://support.microsoft.com/?kbid=244435
 
Upvote 0
To Andrew:
[
Code:
MsgBox ActiveSheet.UsedRange.Address
did not make any difference to the last cell

To Tom:
Code:
Sub Test1() 
Dim LR As Long, LC As Integer, MyRange As Range 
LR = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row 
LC = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 
Set MyRange = Range(Cells(1, 1), Cells(LR, LC)) 
MyRange.Select 'or Copy or whatever 
End Sub
did work and I will probably use it although it seems a bit long winded for something which "should" be simple


I would like to thank you both for your ideas as it proves to me once again that there are very few original problems in Excel and someone always seems to have some idea (you just have to find the right forum)

Thanks again
 
Upvote 0
I agree with Tom about flakiness, but try this experiment.

Open a blank workbook and run this code:

Code:
Sub Test()
   Dim Msg As String
   Msg = "Cells before " & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Address(False, False) & vbCrLf
   Msg = Msg & "UsedRange " & ActiveSheet.UsedRange.Address(False, False) & vbCrLf
   Msg = Msg & "Cells after " & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Address(False, False)
   MsgBox Msg
End Sub

You should get A1 in all instances. Enter something in B5 and run it again. You should get B5 in all instances. Delete the contents of B5 and run it again. You should get B5, A1, A1. So accessing the UsedRange property is resetting the UsedRange.
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,754
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top