简体   繁体   English

如何在命令按钮宏上使用VBA代码打开特定的工作表?

[英]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.

相关问题 如何取消隐藏多个 Excel 文件中的特定工作表。 工作表是使用 vba 隐藏的吗? - how can i unhide a specific worksheet in multiple excel files. the worksheet were hidden using vba? 如何使用VBA打开宏工作簿? - How can I open a macro workbook with VBA? 如何在不离开当前工作表的情况下在特定工作表上运行vba宏 - How to run a vba macro on a specific worksheet without leaving the current worksheet 如何获取工作表代码名称以激活特定工作表? - How can I get worksheet code name to activate a specific worksheet? VBA EXCEL-针对工作表上的打开宏的小代码优化 - VBA EXCEL - Small code optimization for on-worksheet open macro 如果条件满足,我如何使用 VBA 代码复制和粘贴特定单元格到另一个工作表的不同区域 - How can i use VBA Code to Copy and Paste Specific Cells if Condition is Met to different areas of another worksheet 如何在私有宏中插入 vba 代码到命令按钮 - How to insert a vba code in private macro to command button VBA宏可在特定工作表上运行 - VBA Macro to run on a specific worksheet 如何使用在 Excel 上运行的 VBA 宏打开访问工作簿并将表格传输到 Excel 工作表? - How to open an access workbook and transfer a table into an excel worksheet using the VBA macro ran on Excel? 如何将 VBA 代码限制为特定工作表? - How to restrict VBA code to a specific worksheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM