Ms Excel VBA:设置排序组合框

发布于 2024-12-06 18:26:48 字数 282 浏览 0 评论 0原文

尽管我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿嘿嘿 2024-12-13 18:26:48

只是为了用图片来表达,我假设您有一个看起来类似于这样的 Excel 工作表:

Initial ScreenShot of Worksbook

(在此示例中保持 S 列和 R 列可见)

您想要添加一个组合框,该组合框将根据组合框中所选的值对列进行排序,如下所示:

  • 选项 1:按列降序排序R,那么S
  • 选项 2:按 S 列降序排序,然后 R
  • 选项 3:按 A 列降序排序。

如果您还没有这样做,第一件事就是添加Excel 的“开发人员”选项卡

接下来,将表中的单元格放入命名范围中。如果此表中的行将发生更改,请确保创建动态命名范围。 (动态命名范围有点棘手,但对于动态数据非常有用

通过单击开发人员选项卡中的插入来添加组合框,然后从表单中选择组合框控件(注意: ActiveX 组合框是一种完全不同类型的控件。使用它可以得到相同的结果,但代码会有所不同。)

将组合框拖动到工作表:
添加组合框

现在将选项值添加到组合中。您应该转到工作簿中的某个位置并添加组合框的值(例如 Sheet2、单元格 A1、A2 和 A3)。

组合框值

返回表格和组合框所在的工作表。右键单击组合框并选择格式控制

在此处输入图像描述

输入范围范围应该是包含排序选项的单元格。它看起来像这样:Sheet2!$A$1:$A$3

再次右键单击组合框并选择分配宏。为宏命名并将宏放入此工作簿

Macro

单击“新建”。您将进入 Visual Basic 编辑器。

您可以在此处应用排序代码:

Option Explicit

    Sub DropDown2_Change()
        Dim comboValue As String
        Dim Key1ColumnIndex As Integer
        Dim Key2ColumnIndex As Integer

        'You can get the name by doing something like this in the immediate window:  "? Sheet1.Shapes(1).OLEFormat.Object.Name"
        comboValue = Sheet1.Shapes("Drop Down 2").ControlFormat.List(Sheet1.Shapes("Drop Down 2").ControlFormat.ListIndex)

        Select Case comboValue

            Case "Option1"
                Key1ColumnIndex = 18
                Key2ColumnIndex = 19
            Case "Option2"
                Key1ColumnIndex = 19
                Key2ColumnIndex = 18
            Case "Option3"
                Key1ColumnIndex = 1
                Key2ColumnIndex = 1
        End Select


       Range("DataValues").Sort Key1:=Range("DataValues").Cells(1, Key1ColumnIndex), _
                                Order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal, _
                                Key2:=Range("DataValues").Cells(1, Key2ColumnIndex), order2:=xlDescending
    End Sub

您现在应该可以开始了。如果您需要一个工作示例,请查看我在此处创建的示例表;请注意,它没有表的动态名称范围。

Just to put a picture to words, I assume you have an excel sheet that looks similar like this:

Initial ScreenShot of Worksbook

(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:

  • Option 1: Sort descending on column R, then S
  • Option 2: Sort descending on column S, then R
  • Option 3: Sort descending on column A.

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:
Add combo box

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).

Combo box values

Return to your sheet where the table and combo box reside. Right-click on the combo box and select Format Control.

enter image description here

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

Macro

Click New. You will be taken to the Visual Basic Editor.

Here you can apply your sorting code:

Option Explicit

    Sub DropDown2_Change()
        Dim comboValue As String
        Dim Key1ColumnIndex As Integer
        Dim Key2ColumnIndex As Integer

        'You can get the name by doing something like this in the immediate window:  "? Sheet1.Shapes(1).OLEFormat.Object.Name"
        comboValue = Sheet1.Shapes("Drop Down 2").ControlFormat.List(Sheet1.Shapes("Drop Down 2").ControlFormat.ListIndex)

        Select Case comboValue

            Case "Option1"
                Key1ColumnIndex = 18
                Key2ColumnIndex = 19
            Case "Option2"
                Key1ColumnIndex = 19
                Key2ColumnIndex = 18
            Case "Option3"
                Key1ColumnIndex = 1
                Key2ColumnIndex = 1
        End Select


       Range("DataValues").Sort Key1:=Range("DataValues").Cells(1, Key1ColumnIndex), _
                                Order1:=xlDescending, Header:=xlNo, DataOption1:=xlSortNormal, _
                                Key2:=Range("DataValues").Cells(1, Key2ColumnIndex), order2:=xlDescending
    End Sub

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文