Select Text Files from Combobox
Hello again!
Is there a way to read in multiple files of "almost" similar into a combobox
for the user to select the file they want? The filenames first few
characters will be the same but the following characters will be dymanic.
For example:
filenames: SN1033_XXX_67489564926.txt
SN1033_XXX_00909890879.txt
SN1033_XXX_78987907893.txt
SN1008_XXX_78290574238.txt
SN1008_XXX_54545454544.txt
and so on....
So I would like to read all the text filenames beginning with "SN1033". Can
I search a given folder where the files exist for any file beginning with
SN1033 and take all of those file and do a .AddItem for each file to a
UserForm, combobox form, where all the SN1033 files will be listed in the
drop down when the downarrow on the combobox is selected? Thus, the user can
select the file they want given the dynamic variable.
Once that file is selected and becomes ComboBox1.Value, I want to set my
variable TextPath to TextPath = ComboxBox1.Value instead of
"C:\Temp\SN1033_XXX_67489564926.txt". Is that possible?
My current code ==========================
Sub ImportTextStringSN1033()
'Variable Declarations
Dim TextPath As String
Dim NCData As Variant
'===============================================
TextPath = "C:\Temp\SN1033_XXX_67489564926.txt"
On Error GoTo ErrorMsg
Open TextPath For Input As #1 'open text file for SN1033
Do While Not EOF(1) 'go while not end of text file
Line Input #1, NCData
If EOF(1) Then
Application.Range("AP9") = NCData
Range("AP9").Select
'On Error GoTo ErrorMsg1004
'recorded macro for text to columns
Selection.TextToColumns _
Destination:=Range("AP9"), _
DataType:=xlDelimited, _
Comma:=True, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1),
Array(4, 1)), _
TrailingMinusNumbers:=True
End If
Loop
Close #1
Exit Sub
ErrorMsg: 'Error handling routines
Close #1
Range("AP9").Select
Selection.ClearContents
MsgBox "Error " & Err & ": " & Error(Err)
End Sub
=========================================
Thank you all again for any and all assistance!!!!
Kind regards,
D.Parker
|