I have 3 sample files and I want to open/select 2 of the files and copy only the matching data between them into a third Master csv... I have included pictures of the 3 csv files with random data & timestamps. I am new to VBA so my code prolly isn't great but...
File 1. Master Workbook = the xlsm i want to copy the data into
File 2. Target_Time.csv = the csv i want to use to match data (timestamp) against
File 3. Import_Data.csv = the csv i want to copy the row data from if it matches the timestamp in Target_Time.csv
***Note: timestamps in both Import & Target csv's will always be in "ascending" chronological order***
File 1. Master Workbook = the xlsm i want to copy the data into
File 2. Target_Time.csv = the csv i want to use to match data (timestamp) against
File 3. Import_Data.csv = the csv i want to copy the row data from if it matches the timestamp in Target_Time.csv
***Note: timestamps in both Import & Target csv's will always be in "ascending" chronological order***
- So I want the Macro to allow me to select the Target_Time csv & Import_Data csv - these will be a new csv file for each day (24hr period)
- Then I want to select the rows of the Import_Data csv that have a timestamp (Column A in both csv's) matching with the timestamps of the Target_Time cvs.
- Finally, i want to import ONLY those rows from the Import_Data csv into the MasterCSV.
- I have some code already in my macro to adjust the timestamp cells into a "Date" and "Time" column so the copied rows need to start on column 3 of the MasterCSV.
- Finally I would like to create another macro (to use with another button) that saves the MasterCSV with the "date" of the cells in column A (which should all be the same since it's all data from a 24hr period).
VBA Code:
Sub DataLogImport()
Dim ImportFile As String
Dim TargetFile As String
Dim MyDateTime As Date
Dim TargetColumn As Range
TargetFile = Application.GetOpenFilename
If TargetFile = "False" Then
Exit Sub
End If
ImportFile = Application.GetOpenFilename
If ImportFile = "False" Then
Exit Sub
End If
Application.ScreenUpdating = False
'Set the source and target sheets
Set ImportWorkbook = Workbooks.Open(filename:=ImportFile)
Set MasterSheet = ThisWorkbook.Worksheets("Data")
Set TargetWorkbook = Workbooks.Open(filename:=TargetFile)
Set targetSheet = TargetWorkbook.Worksheets(1)
Set TargetColumn = targetSheet.Range("A:A")
'Find the last row in the source sheet
lastRow = ImportWorkbook.Worksheets(1).Cells(ImportWorkbook.Worksheets(1).Rows.Count, "A").End(xlUp).Row
'Find the last row in the Target Sheet
TargetDTRow = TargetWorkbook.Worksheets(1).Cells(TargetWorkbook.Worksheets(1).Rows.Count, "A").End(xlUp).Row
'Find the last row in the master sheet
MasterLastRow = MasterSheet.Cells(MasterSheet.Rows.Count, "C").End(xlUp).Row
'Loop through each row in the source sheet
For i = 2 To TargetTDRow
tempTargetValue = targetSheet.Cells(i, "A").Values
'Check if cell in column A contains "Target Date/Time"
For t = 2 To lastRow
If targetSheet.Cells(i, "A").Values = ImportWorkbook.Cells(t, "A").Values Then
'Copy the entire row to the target sheet
ImportWorkbook.Range("Bt").Copy MasterLastRow.Range("C")
MyDateTime = ImportWorkbook.Worksheets(1).Range("A2").Value
'get date
ThisWorkbook.Worksheets(2).Range("A3").Value = Int(MyDateTime)
ThisWorkbook.Worksheets(2).Range("A3").NumberFormat = "YYYY-MM-DD"
'get time
ThisWorkbook.Worksheets(2).Range("B3").Value = MyDateTime - Int(MyDateTime)
ThisWorkbook.Worksheets(2).Range("B3").NumberFormat = "hh:mm:ss"
End If
Next t
Next i
Application.ScreenUpdating = True
ImportWorkbook.Close
TargetWorkbook.Close
End Sub