简体   繁体   English

如何制作工作表的下拉列表

[英]How to make a drop-down list for worksheets

I have a total of five sheets in a workbook. 我在工作簿中总共有五张纸。 My task is to create a combo list button in the first sheet that will be able to point to the other four. 我的任务是在第一个工作表中创建一个能够指向其他四个的组合列表按钮。 If a user chooses one of the sheet names then the button will automatically activate the chosen sheet. 如果用户选择其中一个工作表名称,则该按钮将自动激活所选工作表。 It is unlikely that sheets will be deleted, though likely that sheets will be added. 虽然可能会添加工作表,但不太可能删除工作表。

I'm not even sure how to get the sheet names to show up on the combo list. 我甚至不确定如何让工作表名称显示在组合列表中。

In order to make the combobox change the active sheet, I believe you'll need to use VBA (as I don't know how to do it using validation lists). 为了使组合框改变活动表,我相信你需要使用VBA(因为我不知道如何使用验证列表)。

To do it, you'll have to: 要做到这一点,你必须:

1st - Add a combobox into your first sheet and properly name it (I called it cmbSheet). 第1步 - 在第一张纸上添加一个组合框并正确命名(我称之为cmbSheet)。 I suggest to use an ActiveX Combobox (in Excel 2007, under Developer tab). 我建议使用ActiveX Combobox(在Excel 2007中,在Developer选项卡下)。

2nd - Open VBA and add the below code into your workbook code. 第2步 - 打开VBA并将以下代码添加到工作簿代码中。 This code will populate the combobox with the sheet names every time the workbook is opened. 每次打开工作簿时,此代码将使用工作表名称填充组合框。

Private Sub Workbook_Open()

    Dim oSheet As Excel.Worksheet
    Dim oCmbBox As MSForms.ComboBox

    Set oCmbBox = ActiveWorkbook.Sheets(1).cmbSheet

    oCmbBox.Clear

    For Each oSheet In ActiveWorkbook.Sheets

        oCmbBox.AddItem oSheet.Name

    Next oSheet

End Sub

3rd - Now, go to the code of your first sheet (where the combobox has been added) and add the code that will activate the sheet chosen in the combobox. 3 - 现在,转到第一张表的代码(已添加组合框的代码)并添加将激活组合框中所选表单的代码。 The code is 代码是

Private Sub cmbSheet_Change()

    ActiveWorkbook.Sheets(cmbSheet.Value).Activate

End Sub

Now, when the combobox value changes, the respective sheet is activated. 现在,当组合框值改变时,相应的纸张被激活。

Let us know if something ins't clear and we'll help you out. 如果有什么不清楚我们会告诉我们,我们会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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