Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browse for file VBA 1

Status
Not open for further replies.

hsviljoen

Programmer
May 26, 2004
73
ZA
I need a control / method to browse for files - similar to VB6's file & directory listboxes.... Any ideas?
 
Use exactly this code.
Code:
  Dim Filter As String, Title As String
  Dim FilterIndex As Integer
  Dim File_Lotus As Variant
    
  ' File filters
  Filter = "Excel Files (*.xls),*.xls," & _
    "Text Files (*.txt),*.txt," & _
    "All Files (*.*),*.*"
  ' Default Filter to *.*
  FilterIndex = 3
  ' Set Dialog Caption
  Title = "Select a File to Open"
  ' Select Start Drive & Path
  ChDrive (ThisWorkbook.Path)
  ChDir (ThisWorkbook.Path)
  With Application
    ' Set File Name to selected File
    File_Lotus = .GetOpenFilename(Filter, FilterIndex, Title)
    ' Reset Start Drive/Path
    ChDrive (Left(.DefaultFilePath, 1))
    ChDir (.DefaultFilePath)
  End With
  ' Open File
  If File_Lotus = False Then
    MsgBox "              No file was selected              "
    Exit Sub
  End If
  
  Workbooks.Open (File_Lotus)
  Set LotusBook = ActiveWorkbook
After that, you can customize by :
- changing the names of the variables
- changing the filters
- changing the default path (line : ChDir ...)

I hope this will work.
Rodie [rednose]
 
Thanks a lot for the Star ... I'm very proud.
Rodie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top