OPEN workbook via combobox

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

OPEN workbook via combobox

Post by sal21 »

I have a userform with combobox1 and near a commanbutton1.
How to show in combobox only all .xls file in c:\mydir\mysubdir\ and after the selection via commandbutton1 open the actuall workbook in new excel session?
Possible.?

User avatar
HansV
Administrator
Posts: 78492
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: OPEN workbook via combobox

Post by HansV »

Put the following code in the userform module:

Code: Select all

Private Const strFolder = "c:\mydir\mysubdir\"

Private Sub CommandButton1_Click()
  If Me.ListBox1.ListIndex = -1 Then
    MsgBox "Please select a filename.", vbExclamation
  Else
    Workbooks.Open strFolder & Me.ListBox1
  End If
End Sub

Private Sub UserForm_Initialize()
  Dim strFile As String
  strFile = Dir(strFolder & "*.xls")
  Do While Not strFile = ""
    Me.ListBox1.AddItem strFile
    strFile = Dir
  Loop
End Sub
Best wishes,
Hans