Hi,
I have a routine that imports a CSV file, based on a user specified file name (project number). It works, but I have a few modifications that I need to do...
1. It MUST account for user error on entering the file name. How can I check to see if the file exists OR break out of the routine on error with a messagebox???
2. The above would be great to know, and would be enough of a solution for me to implement it. BUT if this file name (which is a project number) could be passed through to excel from a web page (a button or something?) then that would be much better. Is this possible? Could the filename (the project number part of it) be passed through via a web page or something?
Code is below, many thanks for any suggestions.
I have a routine that imports a CSV file, based on a user specified file name (project number). It works, but I have a few modifications that I need to do...
1. It MUST account for user error on entering the file name. How can I check to see if the file exists OR break out of the routine on error with a messagebox???
2. The above would be great to know, and would be enough of a solution for me to implement it. BUT if this file name (which is a project number) could be passed through to excel from a web page (a button or something?) then that would be much better. Is this possible? Could the filename (the project number part of it) be passed through via a web page or something?
Code is below, many thanks for any suggestions.
Code:
Sub Acsvimport()
Dim Message, Title, Default, MyValue, infile
Message = "Enter the MG project number" ' Set prompt.
Title = "Input Project Number" ' Set title.
Default = "MGxxxx" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
infile = "H:\EXCEL\projcost\" & MyValue & "-costs.txt"
MsgBox (infile)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' Reads in the CSV file
Sheets("import").Select
Cells.Select
Selection.Delete Shift:=xlUp
Range("a1").Select
Application.DisplayAlerts = True
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & infile, _
Destination:=Range("A1"))
.Name = "projcosts"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1)
.Refresh BackgroundQuery:=False
End With
End Sub