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:
Thank you!
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: