Macro to import a Csv file into an existing workbook - they are both in the same folder

yorkshirelad

Board Regular
Joined
Aug 18, 2003
Messages
100
Apologies for posting again but I urgently need to get this resolved.

I need to import a CSV file (test.csv) into a workbook (data.xlsm) which I have open.

They are both in the same folder.

I cannot use the full directory path as the macro needs to be used on other pcs so the commands must be relative.

I had a suggested answer which brings the data in but it uses a new file rather than the workbook that I have open.

It needs to import into the existing workbook as I have other data in the workbook and macros linked to it

The suggested solution is below but I need it to work as noted above

Sub import_csv_file()
Dim file As String

file = "test.csv"
Workbooks.Open (file)

End Sub


Any assistance would be greatly appreciated.
 
I have a similar problem, however, I don't wish to open the cvs file. The macro below gets the data from DPLOTUS.cvs, but I have to include the path. Since this will be run on other computers, I need to use the folder in which the workbook containing the macro is located.

Sub Import()
'
' import Macro
'
'
Sheets("TB").Select
Columns("A:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A9").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\cbeaulieu\Desktop\DPLOTUS.csv", Destination:=Range("$B$9"))
.Name = "DPLOTUS"
.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, 9, 1, 9, 9, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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