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.
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