简体   繁体   中英

How can I open a specific worksheet using VBA code on a command button macro?

I was wondering how can I open a specific worksheet using VBA code on a command button macro? When then user accesses my "Welcome Sheet" worksheet and clicks on the command button "Students" this should bring them to the "Students" worksheet.

I tried the following code but I got an invalid syntax error message as you can see in this image. Furthermore, some of the code turned to a red code and also one line had a yellow background effect as you can see in this image.

Code:

Private Sub CommandButton_2Click()
Sheets("Students").Activate
End Sub

Incase you wanted to check the command button macro is called the same name as written in the code my excel worksheet is linked below. However, I did check and to me it's all correct.

Video of me showing I have a worksheet called "Students" and I have correctly set up the command button correctly.

Something like:

Private Sub CommandButton2_Click()
    ThisWorkbook.WorkSheets("Students").Activate
End Sub

should do the trick. Remember to fully qualify your references (Eg Workbook --> Sheet here), it'll save you headaches later!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM