[英]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? 我想知道如何在命令按钮宏上使用VBA代码打开特定的工作表? 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. 如果您想检查命令按钮宏的名称与我的excel工作表链接在下面的代码中的名称相同。 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. 我的视频显示我有一个名为“ Students”的工作表,并且我正确地正确设置了命令按钮。
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!
请记住要完全限定您的参考文献(例如,工作簿->表格),这将在以后为您省心!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.