Checking for last populated row in none active worksheet

Thebatfink

Active Member
Joined
Apr 8, 2007
Messages
410
Hi, I'm using the code below to find the row number I want

Code:
With Worksheets("Compiled Sheets")
lastrow = Cells.Find(What:="*", searchorder:=xlByRows, _
 searchdirection:=xlPrevious).Row
End With

But it always gives me the row number of the first sheet in the work book..

I've tried

Code:
Workbooks("name").Sheets("name").Activate

Code:
Workbooks("name").Sheets("name").Select

etc etc but I can't see to make it look at the workbook and sheet I want. Everything else I ever do ussually works fine but obviously theres a reason its defaulting to this work sheet and not my intended one.. I just don't know what it is :) (I'm doing this from a different worksheets code window but I have tried it in a module and it still doesn't want to play ball)..

Any help would be greatly appreciated.

Thanks
Batfink!
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You are missing a dot

Rich (BB code):
With Worksheets("Compiled Sheets")
lastrow = .Cells.Find(What:="*", searchorder:=xlByRows, _
 searchdirection:=xlPrevious).Row
End With
 
Upvote 0
Hi thanks for the reply. With the . I get an error on the lastrow line..

Code:
twfn = Thisworkbook.Name

With Workbooks(twfn).Sheets("My Sheet")
        
lastrow = .Cells.Find(What:="*", searchorder:=xlByRows, _
 searchdirection:=xlPrevious).Row
    
End With

What am I doing wrong here?

Thanks
 
Upvote 0
This works without error and returns the correct value when I run it

Code:
Sub test()
Dim twfn As String, lastrow As Long
twfn = ThisWorkbook.Name

With Workbooks(twfn).Sheets("My Sheet")
        
lastrow = .Cells.Find(What:="*", searchorder:=xlByRows, _
 searchdirection:=xlPrevious).Row
    
End With
MsgBox lastrow
End Sub
 
Upvote 0
I'm putting that into a worksheet module (not the worksheet module of the worksheet I'm trying to find the last row) and its erroring for me..

Run-time error '91':
Object variable or with block variable not set

this is the code

Code:
Private Sub test()
Dim twfn As String
Dim lastrow As Long

twfn = ThisWorkbook.Name

With Workbooks(twfn).Sheets("My Sheet")
        
lastrow = .Cells.Find(What:="*", searchorder:=xlByRows, _
 searchdirection:=xlPrevious).Row
    
End With

MsgBox lastrow

End Sub

I am using Excel 2007 with .xlsm file format?
 
Upvote 0
Oh no, I'm sorry. I've just tried it in a module and it still didn't work. I've just realised the first time its looped through the sheet is blank, but subsequent passes aren't hence checking the row number. Its erroring because the sheet is blank..

Code:
Dim twfn As String
Dim lastrow As Long

twfn = ThisWorkbook.Name

With Workbooks(twfn).Sheets("Compiled Sheets")

On Error Resume Next
      
lastrow = .Cells.Find(What:="*", searchorder:=xlByRows, _
 searchdirection:=xlPrevious).Row

If Err <> 0 Then lastrow = 0
    
End With

MsgBox lastrow

..works much better. Sorry!
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,671
Members
452,937
Latest member
Bhg1984

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