我需要在几张纸上实现同样的排序。此外,我希望用户使用标准排序对话框窗口手动设置排序规则。
因此,我希望创建一个过程,它将启动对话框(XlDialogSort)窗口,在用户指定排序参数并单击OK之后,过程将“读取”这些参数,并通过宏(标准排序代码)将它们应用到多个工作表中。
目前,我的代码如下所示:
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我错过了部分,将获得用户定义的参数按下OK按钮。有什么想法吗?
发布于 2015-10-28 18:50:10
据我所知,您无法从对话框中获取参数。您可以使用Show发送参数,但它们必须是ByVal,因为它们不会更改。排序和查找(可能还有其他一些)参数从一个调用到另一个调用。这意味着下次对相同范围进行排序时,对话框将记住上一次所做的事情。通常,从代码的角度来看,这是危险的,但是您可以在这里使用它。
Dim srt As Sort
If Application.Dialogs(xlDialogSort).Show Then
Set srt = ActiveSheet.Sort
Debug.Print srt.SortFields(1).Key.Address
End If用户为第一个排序字段的键选择的任何内容都将打印到“立即”窗口。您可以在对话框关闭后立即获取排序对象的任何属性,以查看所选内容。显然,将一幅完整的图片组合起来比上面的代码要复杂得多。
https://stackoverflow.com/questions/33395894
复制相似问题