I’m familiar with Word objectmodel and VBA, but not at all with Excel. Using VBA, how can I get the coordinates of the last cell (row/column) that contains a value (is not empty)?
A client receives worksheets that contain a varying number of rows and sometimes 1 or 2 extra columns and she wants to put the Sum() of the last column two rows below the last cell with a value in that column.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Last cell containing value (Win98 / Excel97)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Last cell containing value (Win98 / Excel97)
- This topic has 10 replies, 4 voices, and was last updated 20 years, 9 months ago.
AuthorTopicWSJanB
AskWoody LoungerSeptember 1, 2004 at 8:26 am #409303Viewing 3 reply threadsAuthorReplies-
WSJohnBF
AskWoody LoungerSeptember 1, 2004 at 2:36 pm #871394 -
H. Legare Coleman
AskWoody PlusSeptember 1, 2004 at 5:10 pm #871507John: That method can return unexpected results on at least some versions of Excel (XL2K which I use for example). If rows and/or columns have been deleted from the worksheet, and the workbook has not been saved, then your method can return a cell that is much below and/or to the right of the actual last used cell. The VBA function below should be more reliable:
Public Function FindLastCell(strWorksheet As String) As Range Dim oUsed As Range Dim I As Long, lLastRow As Long, lLastCol As Long, lLast As Long Set oUsed = Worksheets(strWorksheet).UsedRange lLastCol = 0 For I = 0 To oUsed.Rows.Count - 1 lLast = oUsed.Offset(I, oUsed.Columns.Count + 1).End(xlToLeft).Column If lLast > lLastCol Then lLastCol = lLast End If Next I lLastRow = 0 For I = 0 To oUsed.Rows.Count - 1 lLast = oUsed.Offset(oUsed.Rows.Count + 1, I).End(xlUp).Column If lLast > lLastRow Then lLastRow = lLast End If Next I Set FindLastCell = Range("A1").Offset(lLastRow - 1, lLastCol - 1) End Function
-
WSJohnBF
AskWoody LoungerSeptember 1, 2004 at 9:09 pm #871616Function lastusedcell() As String
Dim intCol As Integer
Dim lngRow As Long
For intCol = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
If Application.WorksheetFunction.CountBlank(Columns(intCol)) < ActiveSheet.Rows.Count Then Exit For
Next intCol
For lngRow = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
If Application.WorksheetFunction.CountBlank(Rows(lngRow)) < ActiveSheet.Columns.Count Then Exit For
Next lngRow
lastusedcell = ActiveSheet.Cells(lngRow, intCol).Address
End Function -
WSJohnBF
AskWoody LoungerSeptember 1, 2004 at 9:09 pm #871617Function lastusedcell() As String
Dim intCol As Integer
Dim lngRow As Long
For intCol = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
If Application.WorksheetFunction.CountBlank(Columns(intCol)) < ActiveSheet.Rows.Count Then Exit For
Next intCol
For lngRow = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
If Application.WorksheetFunction.CountBlank(Rows(lngRow)) < ActiveSheet.Columns.Count Then Exit For
Next lngRow
lastusedcell = ActiveSheet.Cells(lngRow, intCol).Address
End Function
-
-
H. Legare Coleman
AskWoody PlusSeptember 1, 2004 at 5:10 pm #871508John: That method can return unexpected results on at least some versions of Excel (XL2K which I use for example). If rows and/or columns have been deleted from the worksheet, and the workbook has not been saved, then your method can return a cell that is much below and/or to the right of the actual last used cell. The VBA function below should be more reliable:
Public Function FindLastCell(strWorksheet As String) As Range Dim oUsed As Range Dim I As Long, lLastRow As Long, lLastCol As Long, lLast As Long Set oUsed = Worksheets(strWorksheet).UsedRange lLastCol = 0 For I = 0 To oUsed.Rows.Count - 1 lLast = oUsed.Offset(I, oUsed.Columns.Count + 1).End(xlToLeft).Column If lLast > lLastCol Then lLastCol = lLast End If Next I lLastRow = 0 For I = 0 To oUsed.Rows.Count - 1 lLast = oUsed.Offset(oUsed.Rows.Count + 1, I).End(xlUp).Column If lLast > lLastRow Then lLastRow = lLast End If Next I Set FindLastCell = Range("A1").Offset(lLastRow - 1, lLastCol - 1) End Function
-
-
WSJohnBF
AskWoody LoungerSeptember 1, 2004 at 2:36 pm #871395 -
WSJim Cone
AskWoody LoungerSeptember 1, 2004 at 9:20 pm #871624There is no need to run a loop…
‘=============================
‘Function BottomRightCorner
‘Jim Cone – San Francisco, CA
‘=============================
Function BottomRightCorner(ByRef objSheet As Worksheet) As Range
On Error GoTo NoCorner
Dim BottomRow As Long
Dim LastColumn As LongIf objSheet.FilterMode Then objSheet.ShowAllData
BottomRow = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set BottomRightCorner = objSheet.Cells(BottomRow, LastColumn)Exit Function
NoCorner:
beep
Set BottomRightCorner = objSheet.Cells(1, 1)
End Function -
WSJim Cone
AskWoody LoungerSeptember 1, 2004 at 9:20 pm #871625There is no need to run a loop…
‘=============================
‘Function BottomRightCorner
‘Jim Cone – San Francisco, CA
‘=============================
Function BottomRightCorner(ByRef objSheet As Worksheet) As Range
On Error GoTo NoCorner
Dim BottomRow As Long
Dim LastColumn As LongIf objSheet.FilterMode Then objSheet.ShowAllData
BottomRow = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set BottomRightCorner = objSheet.Cells(BottomRow, LastColumn)Exit Function
NoCorner:
beep
Set BottomRightCorner = objSheet.Cells(1, 1)
End Function
Viewing 3 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
1 hour, 1 minute ago -
Firefox 139
by
Charlie
17 minutes ago -
Who knows what?
by
Will Fastie
46 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
2 hours, 39 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
10 hours, 54 minutes ago -
Misbehaving devices
by
Susan Bradley
46 minutes ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
1 day, 4 hours ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 hour, 51 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
1 day, 3 hours ago -
Discover the Best AI Tools for Everything
by
Alex5723
2 hours, 20 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
17 hours, 25 minutes ago -
Rufus is available from the MSFT Store
by
PL1
1 day, 1 hour ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
2 days, 4 hours ago -
KB5061768 update for Intel vPro processor
by
drmark
4 hours, 7 minutes ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
2 hours, 50 minutes ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
2 days ago -
Office gets current release
by
Susan Bradley
2 days, 2 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
3 days, 16 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
3 days, 1 hour ago -
Stop the OneDrive defaults
by
CWBillow
3 days, 17 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
4 days, 3 hours ago -
X Suspends Encrypted DMs
by
Alex5723
4 days, 5 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
4 days, 6 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
4 days, 6 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
4 days, 7 hours ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
3 days, 19 hours ago -
Enabling Secureboot
by
ITguy
4 days, 2 hours ago -
Windows hosting exposes additional bugs
by
Susan Bradley
4 days, 15 hours ago -
No more rounded corners??
by
CWBillow
4 days, 10 hours ago -
Android 15 and IPV6
by
Win7and10
4 days ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.