Import csv file and append to existing table?

shellp

Board Regular
Joined
Jul 7, 2010
Messages
194
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
Hello,

The code below works to import my .csv file into Excel onto the RawData_Dis worksheet but I want to change the data on worksheet RawData_Dis into a table and can't find VBA examples to append the new data to the existing table. Note that there are formulae in columns P to X that need to be copied down with the appended data. Any and all assistance to edit the code for appending new data to the table is greatly appreciated.

VBA Code:
Private Sub Import_data_dis()
Dim csvfilename As Variant
Dim destcell As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet

Set ws1 = Sheets("RawData_Dis")
'Set ws2 = Sheets("Data")
Set ws3 = Sheets("Info")

Set destcell = ws1.Cells(Rows.Count, "A").End(xlUp).Offset(1)

chDrive_str = Sheets("ReadMe").Range("C3:C3").Value

Set ofs = CreateObject("scripting.filesystemobject")
ChDrive ofs.getdrivename(chDrive_str)
ChDir chDrive_str

Application.ScreenUpdating = False

csvfilename = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv", Title:="Select a CSV File", MultiSelect:=False)
If csvfilename = False Then Exit Sub

ws1.Visible = -1
ws1.Select

With destcell.Parent.QueryTables.Add(Connection:="Text;" & csvfilename, Destination:=destcell)
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With

last_row = ws1.Cells(Rows.Count, 2).End(xlUp).Row
Sheets("Info").Range("J4:J4").Value = last_row - 1

'this is correct but only if in rawdata worksheet
ws1.Range("P2:X2").AutoFill Destination:=ws1.Range("P2:X" & Cells(Rows.Count, "A").End(xlUp).Row)

ws1.Visible = 0

destcell.Parent.QueryTables(1).Delete

Application.ScreenUpdating = True
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Thanks....but I was hoping to automate the process and I don't think I can do that with power query, correct?
 
Upvote 0
Just came back to thank you alansidman for your suggestion. I didn't know much about power query and your suggestion set me off on a journey to learn about it. I have now reformatted my entire scorecard based on this method and it changes the way I will develop/automate scorecards in the future. Thank you so much.
 
Upvote 0
Happy to hear your success. Thanks for the feedback. You can do a lot with PQ. If you already haven't, you may want to pick up Ken Puls and Miguel Escobar's book.

 
Upvote 0
Thanks!! I was looking for reference material for this...maybe I'll ask Santa :)
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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