Finding last row with data

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Having a bit of trouble... trying
VBA Code:
Sub Go()
Dim x As Range
With Worksheets("A")
Debug.Print .Cells(1, "A")  'this works
Set x = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
Stop
Debug.Print .Range("A1").CurrentRegion.Rows.Count

End With
End Sub

X becomes an unprintable character and last debug.print is 1. Last row is actually 3210.
What am i doing wrong?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Set x = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
I'm not expert at VBA but I think it should be

Set x = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)

This way it will first search down and then move to next column thus giving the right row number
 
Upvote 0
I added 2 debug.print lines:
VBA Code:
Sub Go_1()
Dim x As Range
With Worksheets("A")
Debug.Print .Cells(1, "A")  'this works
Set x = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
Debug.Print x.Address
Debug.Print Len(x)
Stop
Debug.Print .Range("A1").CurrentRegion.Rows.Count

End With
End Sub
what's the result?
 
Upvote 0
I'm not expert at VBA but I think it should be

Set x = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)

This way it will first search down and then move to next column thus giving the right row number
Sanjay, you have it back to front. To find the last row you search ByRows and to find the last column you search ByColumns

 
Upvote 0
Akuini it prints
$A$3210
1
And it's the same whether ByRows or ByColumns is used
VBA Code:
Sub Go_1()
Dim x As Range
With Worksheets("A")
Set x = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
Debug.Print x.Address
Debug.Print Len(x)

Set x = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
Debug.Print x.Address
Debug.Print Len(x)
End With
End Sub
$A$3210
1
$A$3210
1

I now see it's a Range where I was wanting the row number and should have used x.Row.
In an old workbook I had this:
Code:
Public Function LastPopRow(Sheet As String) As Long
LastPopRow = Sheets(Sheet).Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
End Function
Public Function LastPopCol(Sheet As String) As Integer
    LastPopCol = Sheets(Sheet).Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
End Function
Thanks for the replies.
 
Upvote 0
And it's the same whether ByRows or ByColumns is used
Try it with the data layout below ;)

VBA Code:
Sub Go_1()
    Dim x As Range
    With ActiveSheet
        Set x = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        Debug.Print x.Row


        Set x = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
        Debug.Print x.Row

    End With
End Sub

Book1.xlsb
ABCDEFGH
1Order IDOrder DateSiteSP NameCategory NameShip ViaQuantityOrder Total
2104-30-N3130/01/2013On SiteMargaret PeacockMeat/PoultryAir501384.1538
3106-24-F8024/09/2013Off SiteNancy DavolioGrains/CerealsRoad40509.1354
4106-25-F1025/07/2013Off SiteLaura CallahanSeafoodRail21371.6742
5107-21-N1121/10/2013On SiteSteven BuchananSeafoodSea42521.7894
6108-21-N4421/01/2014On SiteLaura CallahanGrains/CerealsSea35866.6508
7106-29-N1429/07/2013On SiteLaura CallahanBeveragesRoad5104.8002
8103-25-N9425/12/2012On SiteNancy DavolioConfectionsSea10483.7476
9103-26-N1526/09/2012On SiteMargaret PeacockBeveragesRail14226.3584
10109-31-F8931/03/2014Off SiteAndrew FullerCondimentsRail401179.6264
11107-4-N2804/11/2013On SiteMargaret PeacockDairy ProductsRail15647.8962
12103-2-N6902/12/2012On SiteLaura CallahanMeat/PoultryRoad20
13105-26-F8126/06/2013Off SiteJanet LeverlingBeveragesRail50
14106-9-F9809/10/2013Off SiteMargaret PeacockCondimentsRoad65
15104-13-F4513/02/2013Off SiteJanet LeverlingMeat/PoultryAir15
16104-31-N9031/03/2013On SiteRobert KingDairy ProductsRoad60
17103-8-F2408/10/2012Off SiteAnne DodsworthDairy ProductsRail40
18104-19-N7919/03/2013On SiteJanet LeverlingBeveragesAir30
19106-9-N9909/10/2013On SiteJanet LeverlingConfectionsRoad12
20107-28-N5828/11/2013On SiteJanet LeverlingConfectionsRoad20
21103-23-N1223/09/2012On SiteAndrew FullerProduceAir4
Sheet3
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,294
Members
449,149
Latest member
mwdbActuary

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