Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Open to specific sheet

0

I'm wanting to open my excel workbook to the sheet that is after another sheet. 
This is the code for opening a specific sheet but I want it to open the sheet that is after "Project Codes"

Private Sub Workbook_Open()

    Worksheets("Project Codes").Activate

End Sub

Answer
Discuss

Answers

0
Selected Answer

 Hello RWSM,

I'm guessing that the worksheet after "Project Codes" changes or you would have simply inserted that sheet name in your code. You need to use the index number of "Project Codes". A sheet's index number refers to its placement in the workbook, the order across the bottom of the workbook. To open the worksheet after  "Project Codes" use this:

Private Sub Workbook_Open()

Dim IndexNbrA As Long

IndexNbrA = Sheets("Project Codes").Index

Worksheets(IndexNbrA + 1).Activate

End Sub

If this solves you problem, please mark my answer as Selected

Cheers   :-)

Discuss

Discussion

@Willie- Nice one. I'd do the similar but avoid two obvious potential errors (that sheet doesn't exist or a user moved it to the end) by adding the bits in bold below

Private Sub Workbook_Open()
 
Dim IndexNbrA As Long

On Error Resume Next
 
IndexNbrA = Sheets("Project Codes").Index

If IndexNbrA < Worksheets.Count Then Worksheets(IndexNbrA + 1).Activate
 
End Sub
John_Ru (rep: 6142) Nov 22, '23 at 6:19 pm
Wow, what timing!  I had that same question TODAY!  Thanks.
SusanUser (rep: 16) Nov 29, '23 at 10:37 am
@Susan- nice of you to have checked (and recognise Willie's effort). When you get to 20 Reputation Points, you will be able to "Vote Up" a useful answer for another user. 

@Willie- happily your Answer has proven useful, even if user RSWM hasn't yet responded.
John_Ru (rep: 6142) Nov 29, '23 at 11:49 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login