Import file, add file name requirement

mdr8c

Board Regular
Joined
Feb 7, 2006
Messages
93
I have a macro that imports a text file into my spreadsheet. I would like to add a feature that checks the name of the file to be imported and ensures that it contains the initials "CD" in the name. That will help ensure the user has selected the correct file to be imported. If "CD" is not part of the file name (the entire file name may be something like "Case 1 CD"), then I would like the code to produce an error message and force the user to select a different file. Can someone please help me add this feature?
Here is the code:

Code:
'Import CD file into database
MsgBox "Select CD text file for import"
Dim sFileName As String
'Collect file name
sFileName = Application.GetOpenFilename("Text Files(*.txt),*.txt")
'If user cancels, get out
If sFileName = "False" Then
Exit Sub
End If
 
ActiveWorkbook.Worksheets("RawDataImport").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & sFileName, Destination:=Range("A1"))
.Name = "CD"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

Thank you!
 
Last edited by a moderator:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
add another if statement
Code:
If InStr(sFileName, "CD") = 0 Then
    MsgBox "Wrong File Chosen"
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top