Importing data to a table

rneilson52

New Member
Joined
Nov 22, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am trying to import data from a CSV file to a table on a separate sheet and paste it into a row starting at column B leaving column A for later use. Would appreciate any assistance for a macro to do this
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You can try this:

VBA Code:
Dim ws As Worksheet, sPath As String
Dim DialogBox As FileDialog
Set DialogBox = Application.FileDialog(msoFileDialogFilePicker)
With DialogBox
.Title = "Select file to import CSV"
.Filters.Add "Comma Separated Files", "*.csv", 1
.Show
End With
If DialogBox.SelectedItems.Count = 1 Then
sPath = DialogBox.SelectedItems(1)
Set ws = ActiveWorkbook.Sheets("Test") 'Name of the sheet where you want to import the CSV file, change this to your case
With ws.QueryTables.Add(Connection:="TEXT;" & sPath, Destination:=ws.Range("b1")) ' first row in column B
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
Else
MsgBox "No files selected !"
End If

Regards,
GB
 
Upvote 0
Are you getting the data from a TEXT file that is to be converted to a CSV file ?

If you are truly accessing a CSV file, can you provide a copy for review ?
 
Upvote 0
You can try this:

VBA Code:
Dim ws As Worksheet, sPath As String
Dim DialogBox As FileDialog
Set DialogBox = Application.FileDialog(msoFileDialogFilePicker)
With DialogBox
.Title = "Select file to import CSV"
.Filters.Add "Comma Separated Files", "*.csv", 1
.Show
End With
If DialogBox.SelectedItems.Count = 1 Then
sPath = DialogBox.SelectedItems(1)
Set ws = ActiveWorkbook.Sheets("Test") 'Name of the sheet where you want to import the CSV file, change this to your case
With ws.QueryTables.Add(Connection:="TEXT;" & sPath, Destination:=ws.Range("b1")) ' first row in column B
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With
Else
MsgBox "No files selected !"
End If

Regards,
GB
OK that is working however it is overwriting the previous import. There are 4 CSV files to import and they have to all show in the table ??
 
Upvote 0
Are you getting the data from a TEXT file that is to be converted to a CSV file ?

If you are truly accessing a CSV file, can you provide a copy for review ?
It is an actual CSV file. I'll need to review the upload process of the files
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,579
Members
452,652
Latest member
eduedu

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