How can I make a UserForm display all the files within a given directory and allow the user to choose one of them? I have a piece of code which makes Excel open a CSV text file. It works flawlessly but it requires the user to type into a textbox the exact name of the file she wants to open. I'd rather she just be able to pick one from some kind of drop-down list. I'm not using the FileDialog object because I don't see how it could allow me to control the type of formatting (text, date, currency, etc.) that Excel uses for each column. Is there a way to use a ComboBox or ListBox automatically populated with the names of the files in the current directory instead of using the TextBox? Or, is there a way for FileDialog to open a file using my desired formatting of each column?
This is the code I have so far. I adapted most of it from a macro I recorded:
I doubt I'm the first person to post a query along these lines. I looked but couldn't find an example that had just what I needed. I may lack the experienced needed to recognize the answer when I see it. Or maybe I wasn't searching for the right terms.
thanks,
cb
This is the code I have so far. I adapted most of it from a macro I recorded:
Code:
Private Sub cmdOpenFile_Click()
ChDrive "P"
' The directory containing the files from which the user must choose:
ChDir "P:\Central Records\cvs from Siebel"
Workbooks.OpenText Filename:= _
txtFileName.Text, Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
'This FieldInfo array is where my column formatting choices are coded.
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 2), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), Array(21, 1), Array(22, 1), _
Array(23, 1), Array(24, 1))
End Sub
I doubt I'm the first person to post a query along these lines. I looked but couldn't find an example that had just what I needed. I may lack the experienced needed to recognize the answer when I see it. Or maybe I wasn't searching for the right terms.
thanks,
cb