是否可以确定所选的 Excel 自定义视图

是否可以确定所选的 Excel 自定义视图

我知道我可以使用 vba 在 excel 中显示自定义视图。我需要做的是确定用户选择了哪个自定义视图,然后根据该选择更改列标题。

他们利用这些信息为客户生成价格表,其中许多客户具有相同的参数,但名称不同。

有没有办法确定所选的自定义视图?Excel 2003

我改变了这个问题的标题,因为它更直接地询问我想要实现什么

我不需要捕获 CustomView 选择事件,我只需要知道选择了哪个 CustomView。您可以确定 Count 和许多其他内容,但显然无法确定 CustomView CommandBar 控件下拉列表中显示的内容

答案1

您可以创建一个“钩住”按钮单击的类。然后,您可以在 Workbook Activate 和 Deactivate 事件中实例化和销毁该类。有三个步骤:

1) 创建一个名为“cCustomViewButton”的类模块。在类模块中粘贴以下代码:

Private WithEvents cmdCustomView As Office.CommandBarButton

Private Sub Class_Initialize() '950 is the relevant control ID
Set cmdCustomView = Application.CommandBars.FindControl(ID:=950)
End Sub

Private Sub cmdCustomView_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
CancelDefault = True
msgbox "you'll have to insert your own custom view picker here"
End Sub

2) 在常规模块中声明一个公共变量,它是您创建的类的一个实例。您可以将其粘贴到任何常规模块中。它需要位于任何例程之前的顶部:

Public clsCustomViewButton As cCustomViewButton

3) 最后,您需要实例化和销毁该类的代码。您将其放在 ThisWorkbook 类中。我喜欢使用 Workbook Activate 和 Deactivate 事件:

Private Sub Workbook_Activate()
Set clsCustomViewButton = New cCustomViewButton
End Sub

Private Sub Workbook_Deactivate()
Set clsCustomViewButton = Nothing
End Sub

现在只需停用然后激活工作簿即可。当您单击“视图”>“自定义视图...”按钮(或使用单击它的键盘快捷键)时,单击事件将运行。

遗憾的是,您会看到我替换了代码中确定当前自定义视图的部分,并将其替换为一个消息框,提示您需要创建自己的自定义视图选择器表单。最初我只是弹出内置的自定义视图对话框,但遗憾的是似乎没有办法确定当前的自定义视图,因此您必须向他们提供选择,然后根据他们选择的内容继续操作。

编辑:

如果您确实想要创建自己的自定义视图选择器,这里有一些基本的代码示例:

Sub ListCustomViews()
Dim wb As Excel.Workbook
Dim cvCustomView As Excel.CustomView

Set wb = ThisWorkbook
For Each cvCustomView In ThisWorkbook.CustomViews
Debug.Print cvCustomView.Name
Next cvCustomView
End Sub

Sub RenameCustomView()
Dim NewName As String

With ThisWorkbook.CustomViews("Old Name")
    .Show
    NewName = "new Name " & .Name
    .Delete
End With
ThisWorkbook.CustomViews.Add (NewName)
End Sub

编辑:

我刚刚注意到您的问题已更新。全面披露:我从未使用过自定义视图,因此没有意识到有两种类型的自定义视图控件 - 视图菜单上的 msoControlButton 和您感兴趣的 msoControlComboBox。需要明确的是,这两种控件都可以出现在工具栏上(如格式化)或下拉菜单中(如视图菜单)。

幸运的是,组合框可以让你做你想做的事。我修改了我原来的 clsCustomViewButton 代码,现在它挂钩了两种类型的控件。对于按钮类型,它只是将用户引导到组合框类型。对于组合框类型,它会弹出一个带有所选类型的消息框。

Private WithEvents cmdCustomView As Office.CommandBarButton
Private WithEvents cboCustomView As Office.CommandBarComboBox

Private Sub Class_Initialize() '950 is the relevant control ID
Set cboCustomView = Application.CommandBars.FindControl(Type:=msoControlComboBox, ID:=950)
Set cmdCustomView = Application.CommandBars.FindControl(Type:=msoControlButton, ID:=950)
End Sub

Private Sub cmdCustomView_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
CancelDefault = True
MsgBox "please use the Custom View dialog on the Formatting Commandbar"
End Sub
Private Sub cboCustomView_Change(ByVal Ctrl As Office.CommandBarComboBox)
MsgBox Ctrl.Text
End Sub

您可能需要确认下拉类型控件是否存在,如果不存在则添加它。

有关这方面的更多想法,请阅读专业 Excel 开发第 232 页及以后,一本你喜欢的书!

绝对是我最后的编辑:

如果您确实不需要捕获下拉列表更改事件而只是希望能够确定当前选定的视图,并假设 Excel 菜单中的某个位置至少有一个自定义视图下拉列表/组合框,则仅此行就可以完成此操作:

应用程序.CommandBars.FindControl(类型:=msoControlComboBox,Id:=950).文本

呼!

相关内容