Excel 具有重要工作簿概述和打开它们的功能的Listbox加载项

Excel 具有重要工作簿概述和打开它们的功能的Listbox加载项,excel,vba,listbox,userform,Excel,Vba,Listbox,Userform,我想创建一个宏,我可以与同事共享,其中有一个userformlistbox,包含我们所有重要工作簿的简单概述 我刚开始使用VBA Excel,很难弄清楚程序是如何工作的。我在这个问题上搜索了很多,但是没有找到我可以使用的东西。我认为解决办法相当简单 Private Sub ListBox1_Click() End Sub 本质上,我只需要一个带有我定义的工作簿列表框的用户表单。工作簿位于不同的文件夹结构中,因此我需要为每个工作簿设置路径 用户表单应该有一个名为“打开”的命令单击框,用户在选择

我想创建一个宏,我可以与同事共享,其中有一个userformlistbox,包含我们所有重要工作簿的简单概述

我刚开始使用VBA Excel,很难弄清楚程序是如何工作的。我在这个问题上搜索了很多,但是没有找到我可以使用的东西。我认为解决办法相当简单

Private Sub ListBox1_Click()

End Sub
本质上,我只需要一个带有我定义的工作簿列表框的用户表单。工作簿位于不同的文件夹结构中,因此我需要为每个工作簿设置路径


用户表单应该有一个名为“打开”的命令单击框,用户在选择要打开的工作簿时单击该框。

通过用户表单列表框打开工作簿

在一个非常基本的表单中,您可以在UserForm模块中使用以下代码:;当然,您可以/应该将使用过的控件重命名为更能说话的名称(例如,“OpenFileButton”而不是CommandButton1)。在双击事件中还添加了“打开文件”功能

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

谢谢这很有帮助!我发现工作簿.Open方法有错误(编译错误:找不到方法或数据成员)。相比之下,我有另一个.xlsm文件,在Workbooks.Open中我没有得到错误。然而,这个文件来自于2008年某论坛上的一篇帖子以及97-2003版本的Excel。你知道我为什么会出现这个错误吗?我在这里找到了我评论的解决方案:你知道有没有办法命名工作簿项?现在我有文件夹结构链接作为列表项的名称,但是我希望它只是excel工作簿的名称。有什么想法吗?建议在做一些研究,因为这将是一个广泛的答案-这里有许多解决方案如何搜索文件夹和子文件夹使用例如
Dir
功能。使用ADODB的更复杂的方法可能会给你一些额外的想法(例如:-)我当然不是专家,但是我不相信我正在尝试做的事情是非常困难的。我有一个列表框,其中包含一组指向excel文件的X路径,我可以使用上面的代码打开这些文件。它们看起来像这样:C:\Users\UserName\Data\interest.xlsx,C:\Users\UserName\Data\Trades.xslx。。。与其在列表框中显示完整的文件路径,我宁愿让它显示:兴趣,交易,当你按下它时,你通过“访问”文件路径打开文件
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
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