การเขียน VBA ให้คลิกที่ปุ่มแล้วลิงก์ไปยังชีทที่เราต้องการ
Table of Contents
Introduction
This tutorial will guide you through the process of using VBA in Excel to create a button that links to a specific worksheet. Additionally, for those who prefer a simpler method, we will explore an alternative approach using hyperlinks. This tutorial is perfect for anyone looking to enhance their Excel sheets with interactive features.
Step 1: Setting Up Your Excel Workbook
- Open your Excel workbook where you want to add the button or hyperlink.
- Ensure you have multiple sheets created, as you will be linking to one of them.
Step 2: Creating a Button for VBA
- Go to the Developer tab in the Ribbon.
- If the Developer tab is not visible, enable it by going to File > Options > Customize Ribbon and checking the Developer option.
- Click on Insert and choose Button (Form Control).
- Draw the button on your worksheet.
- A dialog box will appear prompting you to assign a macro. Click on New to create a new macro.
Step 3: Writing the VBA Code
-
In the VBA editor that opens, you'll see a subroutine created for your button. Add the following code to navigate to the desired sheet:
Sub Button1_Click() Sheets("SheetName").Activate End Sub
- Replace
"SheetName"
with the actual name of the sheet you want to link to.
- Replace
-
Close the VBA editor by clicking on the X in the upper right corner.
Step 4: Testing Your Button
- Return to your Excel workbook.
- Click the button you created. It should take you directly to the specified worksheet.
Step 5: Creating a Hyperlink Alternative
If you prefer not to use VBA, you can create a hyperlink instead:
- Select a cell where you want to create the hyperlink.
- Right-click and choose Hyperlink from the context menu.
- In the Insert Hyperlink dialog:
- Select Place in This Document on the left.
- Choose the sheet you want to link to and enter a friendly name for the link.
- Click OK to create the hyperlink.
Conclusion
In this tutorial, you learned how to create an interactive button using VBA to link to a specific worksheet in Excel, as well as an alternative method using hyperlinks. Both approaches enhance the functionality of your workbook, making navigation easier for users. Experiment with both methods to see which one works best for your needs!