LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 153
Default Last Cell

Hi everybody,

I'm relatively new to programming with VBA (Excel 97), but it
didn't take me long to discover that Excel had a problem with the
"last cell" at times. Browsing the Excel sites, including this
group, confirmed that this was a known issue, and offered several
workarounds.

One of them was to reset the last cell using the statement
"ActiveSheet.UsedRange" before
"ActiveSheet.Cells.SpecialCells(xlLastCell).Select ". I find that
this works some of the time, but not if one of the cells outside the
data area is formatted differently from the others.

To date, the only thing that seems to work consistently for me is a
routine similar to the following (which I am NOT taking credit for,
it's a modification of code I found on the web).

Private Sub realLastCell()

Dim rCnt As Long
Dim cCnt As Integer

' What Excel thinks is the last cell
ActiveSheet.Cells.SpecialCells(xlLastCell).Select

rCnt = ActiveSheet.Cells.SpecialCells(xlLastCell).ROW
cCnt = ActiveSheet.Cells.SpecialCells(xlLastCell).Column

' Determine the last row with containing data
Do While Application.CountA(ActiveSheet.ROWS(rCnt)) = 0 And
rCnt < 1
rCnt = rCnt - 1
Loop

' Determine the last column containing data
Do While Application.CountA(ActiveSheet.Columns(cCnt)) = 0 And
cCnt < 1
cCnt = cCnt - 1
Loop
Cells(rCnt, cCnt).Select
End Sub

Consequently, whenever it's critical that an application identifies
the last cell, I include code similar to the above, just to be sure.

I guess I would like to know if this is considered a bit "klunky",
or if experienced programmers would choose a similar method.

Thanks,

DaveU

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to copy the formulae of one cell to all the cell in the rangewith the specific cell and columnnumber changing Options Yuvraj Excel Discussion (Misc queries) 0 June 29th 09 11:20 AM
Code to copy the formulae of one cell to all the cell in the rangewith the specific cell and columnnumber changing Yuvraj Excel Discussion (Misc queries) 0 June 26th 09 06:01 PM
Populate a cell if values in cell 1 and cell 2 match cell 3 and 4 [email protected] Excel Worksheet Functions 1 August 22nd 08 02:04 AM
How to create/run "cell A equals Cell B put Cell C info in Cell D abmb161 Excel Discussion (Misc queries) 5 January 26th 06 06:36 PM
data validation to restrict input in cell based on value of cell above that cell NC Excel Programming 2 January 25th 05 07:11 AM


All times are GMT +1. The time now is 10:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"