Append last rows from csv file to Master xlsx file based on csv timestamp

UseLessFuel

New Member
Joined
Dec 22, 2012
Messages
37
Hi.
I occasionaly download a .csv file which contains exactly one year of 1-minute interval data, held in 8 columns (A to H) and copy the data that I do not already have into a Master .xlsx file. The csv data looks like this:

ABCDEFGH
1
TimestampZ1Z2OutFloEnCEnpOpMo
209/05/2017 14:50
17181030824water
309/05/2017 14:5117171028927water

<tbody>
</tbody>

The Master .xlsx file looks the same (same variables in same columns) but I only need to append the latest data (based on the timestamp) from the csv file into the xlsx file, which is typically the last 43,000 rows of the csv data, at a time.

I was kindly provided with VBA code (below) which has worked, but I think the loop takes too long to execute when there is a large number of rows to copy. Does anyone know of a more efficient way to append only the latest csv data into an xlsx file?

'Appends new data to Master files. You need to have the Master file open
'as well as the downloaded csv file
'

Sub Append()

Dim curworkbook As Workbook
Dim curworksheet As Worksheet
Dim wb As Workbook
Dim ws As Worksheet
Dim Curlastrow As Long
Dim CSVlastrow As Long
Curlastrow = Cells(Rows.Count, 1).End(xlUp).Row
'
'Copy the filename of the Workbook,and overwrite SH005 Trial VBA.xlsx, below
'

Set curworkbook = Workbooks("SH005 Trial VBA.xlsx")
'
'Copy the name of the Worksheet by double clicking the worksheet tab and pressing Ctrl C.
'Now paste it on top of Master data, below
'

Set curworksheet = Worksheets("Master data")
'
'You should not have to change the Workbook and Worksheet filenames below as
'they are all the same when you first download the csv
'

Set wb = Workbooks("CSVReport.csv")
Set ws = wb.Worksheets("CSVReport")
CSVlastrow = ws.Range("A" & Rows.Count).End(xlUp).Row
maxdate = Application.WorksheetFunction.Max(Range(Cells(2, 1), Cells(Curlastrow, 1)))
For i = 2 To CSVlastrow
If ws.Cells(i, 1).Value > maxdate Then
ws.Cells(i, 1).EntireRow.Copy Destination:=curworksheet.Cells(Curlastrow + 1, 1)
Curlastrow = Curlastrow + 1
End If
Next i
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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