Select last sheet in wb when sheets are named

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
506
Office Version
  1. 365
Platform
  1. Windows
Hi folks
Trying to find a way to simply select/activate the last sheet in a workbook (Last sheet extreme rh side of tabs, not last sheet used)
Sheets(Sheets.Count).Select doesn't seem to work, being as I am dynamically renaming each sheet as it is created, so I do not have Sheet1, Sheet2 etc.
Is there another way?
Thanks
 
'Runtime error 9 Subscript out of range'
Is normally a naming error, check the number of spaces (including leading and trailing spaces in the workbook name (and try with the file extension as @Fluff has suggested)
'Runtime 1004 Select method of Worksheet class failed'

Do you have any workbook or worksheet protection active?

This line would probably mean yes
VBA Code:
MsgBox "WRONG PASSWORD.... BUGGER OFF!!", vbCritical + vbOKOnly, "Access denied"
 
Upvote 1

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
The full code is below, I'm using a UserForm as a password entry and depending on the password acceptance, there are further function.

VBA Code:
Private Sub CommandButton1_Click()
If TextBox1.Value = "Name" Then

Worksheets("Employee List").Range("J10").Value = "Name"

Worksheets("Input").Activate

ActiveWindow.DisplayWorkbookTabs = True

Unload Me

UserFormMain.Hide

With Workbooks("HOLIDAY BOOKING")
   .Activate
   .Worksheets(.Worksheets.Count).Select
End With

Else
MsgBox "WRONG PASSWORD.... BUGGER OFF!!", vbCritical + vbOKOnly, "Access denied"
Unload Me



End If

End Su
[/QUOTE]

Try adding the file extension the workbook name.
That gets past the previous error, but then just hangs on the Worksheets line with the runtime 1004 error
 
Upvote 0
Is normally a naming error, check the number of spaces (including leading and trailing spaces in the workbook name (and try with the file extension as @Fluff has suggested)


Do you have any workbook or worksheet protection active?

This line would probably mean yes
VBA Code:
MsgBox "WRONG PASSWORD.... BUGGER OFF!!", vbCritical + vbOKOnly, "Access denied"
"Do you have any workbook or worksheet protection active?"
Yes, I do..... I'll have a play with that
 
Upvote 0
Bugger! Sussed it, I think. Your question on protection made me twig. I forgot that I have 2 sheets hidden!!! Doh! I just unhid them and it works fine. I'll add a line to unhide, select last sheet then hide again. Hopefully that should sort it.
Most of the problems I have are self-inflicted daftness
 
Upvote 0
Yep, sorted. My own stupid fault, as usual.
VBA Code:
Private Sub CommandButton1_Click()
If TextBox1.Value = "name" Then

Worksheets("Employee List").Range("J10").Value = "Name"

Worksheets("Input").Activate

ActiveWindow.DisplayWorkbookTabs = True

Unload Me

UserFormMain.Hide

Dim ws As Worksheet
   
    For Each ws In ActiveWorkbook.Worksheets
       
        ws.Visible = xlSheetVisible
   
    Next ws

Sheets(Sheets.Count).Select

Else
MsgBox "WRONG PASSWORD.... BUGGER OFF!!", vbCritical + vbOKOnly, "Access denied"
Unload Me



End If

End Sub
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0
Thank you all for your time, you lot usually either solve my issues instantly or push me into the right place to spot my own dumbass errors. To be fair, most of my VBA issues are pretty basic
 
Upvote 0
If you didn't want to unhide them you could try something like this.

VBA Code:
Sub GetLastVisibleSheet()

    Dim iSht As Long
    Dim lastSht As Worksheet
   
    With Workbooks("HOLIDAY BOOKING.xlsx")              ' Change extension to correct extension
       .Activate
      
       Set lastSht = .Worksheets(.Worksheets.Count)
       If lastSht.Visible = False Then
            For iSht = (.Worksheets.Count - 1) To 1 Step -1
                If .Worksheets(iSht).Visible = True Then
                    Set lastSht = .Worksheets(iSht)
                    Exit For
                End If
            Next iSht
        End If
        lastSht.Select
    End With

End Sub
 
Upvote 0
If you didn't want to unhide them you could try something like this.

VBA Code:
Sub GetLastVisibleSheet()

    Dim iSht As Long
    Dim lastSht As Worksheet
  
    With Workbooks("HOLIDAY BOOKING.xlsx")              ' Change extension to correct extension
       .Activate
     
       Set lastSht = .Worksheets(.Worksheets.Count)
       If lastSht.Visible = False Then
            For iSht = (.Worksheets.Count - 1) To 1 Step -1
                If .Worksheets(iSht).Visible = True Then
                    Set lastSht = .Worksheets(iSht)
                    Exit For
                End If
            Next iSht
        End If
        lastSht.Select
    End With

End Sub
Ooh, that's pretty...... and it works too. Thank you :)
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,782
Members
449,123
Latest member
StorageQueen24

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