strange behaviour from SpecialCells(xlLastCell)

obiron

Active Member
Joined
Jan 22, 2010
Messages
469
Hi guys , I have the folowing code in a workbook

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim cell As Range
    Dim DupAssetRows As String
    Dim NonNumvericValues As String
    Stop
    
    Sheets("Copied from scheme files").Select
    Range("A1").Select
    ActiveCell.SpecialCells(xlLastCell).Select
.....
End Sub

When I open this workbook manually, ActiveCell.SpecialCells(xlLastCell).address gives me a location of $AG$10849

When I open it using VBA code and then close it again the beforeClose event fires but the ActiveCell.SpecialCells(xlLastCell).address gives me $A$1 and the validation that occurs in the remainder of the procedure fails.

I'm going to change it to use UsedRange instead but wondered why I get different behaviour from the two opening methods
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi

I wouldn't use either the SpecialCells method or the UsedRange property of a worksheet to determine where the last cell might be - I would use the Find method:

Code:
Dim rLastCell As Range
 
Set rLastCell = Sheets("Copied from scheme files").Cells.Find(What:="*",Lookin:=xlFormulas,LookAt:=xlWhole,SearchOrder:=xlByRows,SearchDirection:=xlPrevious)
 
'now you have a range reference to a cell in the last used row in the sheet - do something with it:
 
rLastCell.Activate

Note: usual caveats over you usually don't need to Select/Activate cells to use them applies.
 
Upvote 0
Thanks for the tip Richard,

The problem appears to be that the select commands are not working (not my code - I don't use selects!!!)

I have now re-written the code to define two range variables and use those instead.
 
Upvote 0

Forum statistics

Threads
1,225,075
Messages
6,182,727
Members
453,132
Latest member
nsnodgrass73

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