获取VBA对话框的选项(xlDialogSort)

我需要在几张纸上实施相同的sorting。 此外,我希望用户使用标准的sorting对话窗口手动设置sorting规则。

所以,我想创build一个启动对话框(xlDialogSort)窗口的过程,并且在用户指定了sorting参数并点击确定之后,程序将“读取”这些参数并通过macros(标准sorting代码)将它们应用到多个表单中。

目前我的代码如下所示:

Public Sub sortMultipleSheets() Dim sortDiagAnswer As Boolean sortDiagAnswer = Application.Dialogs(xlDialogSort).Show If Err.Number = 1004 Then MsgBox "Place the cursor in the area to be sorted" Exit Sub End If Err.Clear If sortDiagAnswer Then 'read user defined parameters '... actualSort (wsh1) actualSort (wsh2) End If End Sub Private Sub actualSort(ByVal wsh As Worksheet) With wsh.Sort With .SortFields .Clear .Add Key:= 'user-defined key1 .Add Key:= 'user-defined key2 .Add Key:= 'user-defined key3 'any more user-defined keys End With .SetRange Range('actual range) .Header = 'setting from the sorting dialog window .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With End Sub 

我错过了将获得用户定义的参数点击确定button的部分。 有什么想法吗?

据我所知,你不能从对话框中获取参数。 您可以使用Show方法发送参数,但是它们必须是ByVal,因为它们不会更改。 sorting和查找(也可能是其他的)参数从一个调用持续到另一个调用。 这意味着下一次对相同范围进行sorting时,对话框将记住您之前所做的操作。 通常从代码的angular度来看这是危险的,但是你可以在这里使用它。

 Dim srt As Sort If Application.Dialogs(xlDialogSort).Show Then Set srt = ActiveSheet.Sort Debug.Print srt.SortFields(1).Key.Address End If 

无论用户为第一个sorting字段的键select什么,都将打印到立即窗口。 您可以在对话框closures后立即获取Sort对象的任何属性,以查看所选内容。 显然整个画面会比上面的代码更复杂。