Hi I have a macro I found online but would like to make modifications to it.
1st would like to know if I can modify so that it brings me directly to directory I want where my csv files are. ex my files are in E:\financialexcel\transactions so when I click on macro I want it to take me directly to that folder then I can choose file I want to import.
2. I need to import different files therefore would like to import 2nd file right after data from first file.
Also noticed if I run the macro more than once I get a runtime error 9, subscript out of range and the following line is highlighted in yellow
ActiveWorkbook.Sheets(2).QueryTables("importCSVimporter").Delete
here is the macro I am using. thanks
1st would like to know if I can modify so that it brings me directly to directory I want where my csv files are. ex my files are in E:\financialexcel\transactions so when I click on macro I want it to take me directly to that folder then I can choose file I want to import.
2. I need to import different files therefore would like to import 2nd file right after data from first file.
Also noticed if I run the macro more than once I get a runtime error 9, subscript out of range and the following line is highlighted in yellow
ActiveWorkbook.Sheets(2).QueryTables("importCSVimporter").Delete
here is the macro I am using. thanks
VBA Code:
Sub importCSV()
Dim column_types() As Variant
csv_path = Application.GetOpenFilename()
If csv_path = False Then
Exit Sub
End If
For i = 0 To 16384
ReDim Preserve column_types(i)
column_types(i) = 2
Next i
With ActiveWorkbook.Sheets(2).QueryTables.Add(Connection:="TEXT;" & csv_path, Destination:=Range("A1"))
.Name = "importCSVimporter"
.FieldNames = True
.AdjustColumnWidth = True
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = column_types
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Sheets(2).QueryTables("importCSVimporter").Delete
End Sub