列表框插件,包含重要工作簿的概览和打开它们的能力
Listbox add-in with an overview of important workbooks and ability to open them
我想创建一个宏,我可以使用用户窗体列表框与同事共享,其中包含我们拥有的所有重要工作簿的简单概述。
我刚开始使用 VBA Excel 并且无法理解该程序的工作原理。我在这个问题上搜索了很多,但找不到我可以使用的东西。我怀疑解决方案非常简单。
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "WB1"
ListBox1.AddItem "WB2"
ListBox1.AddItem "WB3"
End Sub
本质上,我只想要一个带有我定义的工作簿列表框的用户窗体。工作簿位于不同的文件夹结构中,因此我需要为每个单独的工作簿设置路径。
用户窗体应该有一个名为 "Open" 的命令单击框,用户在选择他或她要打开的工作簿时单击该框。
通过用户窗体列表框打开工作簿
在一个非常基本的表单中,您可以在用户表单模块中使用以下代码;当然,您 can/should 将使用过的控件重命名为更 会说话的 名称(例如 "OpenFileButton" 而不是 CommandButton1)。也为双击事件添加了 "Open file" 功能。
Option Explicit ' declaration head of userform code module
Private Sub doOpenSelectedWB()
' Purpose: add open file functionality (called by CommandButton1 and Listbox1's double click event)
With Me.ListBox1
' [1] Escape proc if no valid item
If .ListIndex < 0 Or .ListCount = 0 Then Exit Sub
' [2] Get selected workbook name via list value (index 0 equals 1st column)
Dim myFileName As String
myFileName = .List(.ListIndex, 0)
' [3] Open existing workbooks via Workbooks.Open method
If WBExists(myFileName) Then Workbooks.Open myFileName
End With
End Sub
Private Sub CommandButton1_Click()
doOpenSelectedWB
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
doOpenSelectedWB
End Sub
Private Sub UserForm_Initialize()
' Purpose: prefill listbox with array values
Dim myWBArray()
myWBArray = Array("C:\Users\Admin\documents\test.xlsx", "C:\Users\Admin\documents\test2.xlsx", "C:\Users\Nowhere\nonsens.xlsm")
Me.ListBox1.List = myWBArray
End Sub
辅助函数WBExists()
和命令按钮的格式
Private Function WBExists(Fullname$, Optional ByVal Infotext$ = "File not found!") As Boolean
' Purpose: Check if workbook exists
If Dir(Fullname) = vbNullString Then
MsgBox "File " & Fullname & " not found!", vbExclamation, Infotext
Else
WBExists = True
End If
End Function
Private Sub UserForm_Layout()
' Purpose: name command button
Me.CommandButton1.Caption = "Open"
End Sub
我想创建一个宏,我可以使用用户窗体列表框与同事共享,其中包含我们拥有的所有重要工作簿的简单概述。
我刚开始使用 VBA Excel 并且无法理解该程序的工作原理。我在这个问题上搜索了很多,但找不到我可以使用的东西。我怀疑解决方案非常简单。
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "WB1"
ListBox1.AddItem "WB2"
ListBox1.AddItem "WB3"
End Sub
本质上,我只想要一个带有我定义的工作簿列表框的用户窗体。工作簿位于不同的文件夹结构中,因此我需要为每个单独的工作簿设置路径。
用户窗体应该有一个名为 "Open" 的命令单击框,用户在选择他或她要打开的工作簿时单击该框。
通过用户窗体列表框打开工作簿
在一个非常基本的表单中,您可以在用户表单模块中使用以下代码;当然,您 can/should 将使用过的控件重命名为更 会说话的 名称(例如 "OpenFileButton" 而不是 CommandButton1)。也为双击事件添加了 "Open file" 功能。
Option Explicit ' declaration head of userform code module
Private Sub doOpenSelectedWB()
' Purpose: add open file functionality (called by CommandButton1 and Listbox1's double click event)
With Me.ListBox1
' [1] Escape proc if no valid item
If .ListIndex < 0 Or .ListCount = 0 Then Exit Sub
' [2] Get selected workbook name via list value (index 0 equals 1st column)
Dim myFileName As String
myFileName = .List(.ListIndex, 0)
' [3] Open existing workbooks via Workbooks.Open method
If WBExists(myFileName) Then Workbooks.Open myFileName
End With
End Sub
Private Sub CommandButton1_Click()
doOpenSelectedWB
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
doOpenSelectedWB
End Sub
Private Sub UserForm_Initialize()
' Purpose: prefill listbox with array values
Dim myWBArray()
myWBArray = Array("C:\Users\Admin\documents\test.xlsx", "C:\Users\Admin\documents\test2.xlsx", "C:\Users\Nowhere\nonsens.xlsm")
Me.ListBox1.List = myWBArray
End Sub
辅助函数WBExists()
和命令按钮的格式
Private Function WBExists(Fullname$, Optional ByVal Infotext$ = "File not found!") As Boolean
' Purpose: Check if workbook exists
If Dir(Fullname) = vbNullString Then
MsgBox "File " & Fullname & " not found!", vbExclamation, Infotext
Else
WBExists = True
End If
End Function
Private Sub UserForm_Layout()
' Purpose: name command button
Me.CommandButton1.Caption = "Open"
End Sub