Ms Excel VBA:设置排序组合框
尽管我在 Visual Basic 方面有一些经验,但我还是 VBA 新手。 我有一个 Microsoft Excel 2010 工作表。第 29 行包含表的标题,数据来自第 30 行,依此类推。该表大约有 20 列。
我试图在此工作表中插入一个包含三个选项的组合,因此当您选择第一个时,它将根据 R 列和 S 列对所有表应用降序排序。如果您选择第二个,它将应用降序排序根据 S 列排序,然后按 R 列排序。如果您选择第一个,它将根据 A 列应用降序排序。 S 列和 R 列将被隐藏。 我希望你们能帮助我。谢谢你,对我的英语感到抱歉。
I am new to VBA although I have some experience in Visual Basic.
I have a Microsoft Excel 2010 worksheet. Row 29 has the headers of a table and the data is from row 30 and so on. This table has like 20 columns.
I'm trying to insert a combo in this Worksheet with three options, so when you choose the first, it will apply descending sorting to all the table according to column R and then column S. If you choose the second, it will apply descending sorting according to column S and then column R. If you choose the first it will apply descending sorting according to column A.
Column S and Column R will be hidden.
I hope you guys can help me out. Thank you and sorry for my English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了用图片来表达,我假设您有一个看起来类似于这样的 Excel 工作表:
(在此示例中保持 S 列和 R 列可见)
您想要添加一个组合框,该组合框将根据组合框中所选的值对列进行排序,如下所示:
如果您还没有这样做,第一件事就是添加Excel 的“开发人员”选项卡。
接下来,将表中的单元格放入命名范围中。如果此表中的行将发生更改,请确保创建动态命名范围。 (动态命名范围有点棘手,但对于动态数据非常有用)
通过单击开发人员选项卡中的插入来添加组合框,然后从表单中选择组合框控件(注意: ActiveX 组合框是一种完全不同类型的控件。使用它可以得到相同的结果,但代码会有所不同。)
将组合框拖动到工作表:
现在将选项值添加到组合中。您应该转到工作簿中的某个位置并添加组合框的值(例如 Sheet2、单元格 A1、A2 和 A3)。
返回表格和组合框所在的工作表。右键单击组合框并选择格式控制。
输入范围范围应该是包含排序选项的单元格。它看起来像这样:Sheet2!$A$1:$A$3
再次右键单击组合框并选择分配宏。为宏命名并将宏放入此工作簿
单击“新建”。您将进入 Visual Basic 编辑器。
您可以在此处应用排序代码:
您现在应该可以开始了。如果您需要一个工作示例,请查看我在此处创建的示例表;请注意,它没有表的动态名称范围。
Just to put a picture to words, I assume you have an excel sheet that looks similar like this:
(Keeping columns S and R visible for this example)
You want to add a combo box that will sort columns based on the value selected in the combo box that will be like so:
First thing, if you haven't already done so, is add the Developer Tab to Excel.
Next, put cells from the table in a Named Range. If the rows in this table will change, then make sure you create a dynamic named range. (Dynamic Named Ranges are a little tricky, but very useful for dynamic data)
Add the combo box by clicking Insert from the Developer Tab and select combo box from Form Controls (NOTE: An ActiveX combobox is a completely different type of control. You could come to the same result using it, but the code would be different.)
Drag the combobox somewhere on the worksheet:
Now add the options values to the combo. You should go somewhere in your workbook and add the values for you combo box (e.g. Sheet2, Cells A1, A2 & A3).
Return to your sheet where the table and combo box reside. Right-click on the combo box and select Format Control.
The input range range should be the cells containing your sorting options. It look something like this: Sheet2!$A$1:$A$3
Right click on the combo box again and select Assign Macro. Give the Macro a name and put the Macro in This Workbook
Click New. You will be taken to the Visual Basic Editor.
Here you can apply your sorting code:
You should be now be good to go. If you need a working example, take a look at the sample sheet I created here; note that it does not have a dynamic name range for the table.