Hi,
I've got code written to import a csv file to a worksheet, the csv file will always be housed in the same folder on our server, the problem is everyone running the code has that paticular server named something different ie X:\Shared or G:\Shared or I:\Shared. I have a code that allows you to choose a file to open and the code to import the csv file from the root directory, problem is I'm not sure how to marry the two. I need the open file dialogue box to open, the user chooses the csv file, then the import continues as normal.
Open File Code:
Sub Openfile()
Dim OFile As String
OFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If OFile = False Then
' They pressed Cancel
MsgBox "You must choose the Accnt Detail CSV file to continue the breakout"
Exit Sub
Else
Workbooks.Open Filename:=OFile
End If
End Sub
Import code:
With ActiveSheet.QueryTables.ADD(Connection:= _
"TEXT;H:\Shared\SI\6K\Sales Incentive Query_6k_CSV.csv", _
Destination:=Range("$A$1"))
.Name = "Sales Incentive Query_Local Sales_6k_CSV"
.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 = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
I've got code written to import a csv file to a worksheet, the csv file will always be housed in the same folder on our server, the problem is everyone running the code has that paticular server named something different ie X:\Shared or G:\Shared or I:\Shared. I have a code that allows you to choose a file to open and the code to import the csv file from the root directory, problem is I'm not sure how to marry the two. I need the open file dialogue box to open, the user chooses the csv file, then the import continues as normal.
Open File Code:
Sub Openfile()
Dim OFile As String
OFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If OFile = False Then
' They pressed Cancel
MsgBox "You must choose the Accnt Detail CSV file to continue the breakout"
Exit Sub
Else
Workbooks.Open Filename:=OFile
End If
End Sub
Import code:
With ActiveSheet.QueryTables.ADD(Connection:= _
"TEXT;H:\Shared\SI\6K\Sales Incentive Query_6k_CSV.csv", _
Destination:=Range("$A$1"))
.Name = "Sales Incentive Query_Local Sales_6k_CSV"
.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 = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With