VBA to copy only matching data from 2 csv workbooks into the "Master Workbook"

Status
Not open for further replies.

DCFreit0s

New Member
Joined
Jul 23, 2024
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
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***
  1. 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)
  2. 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.
  3. Finally, i want to import ONLY those rows from the Import_Data csv into the MasterCSV.
  4. 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.
  5. 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).
PLEASE HELP!!! Ive been googling and testing different codes for days!!!

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
 

Attachments

  • Master_Workbook.png
    Master_Workbook.png
    48.5 KB · Views: 5
  • Target_Time_CSV.png
    Target_Time_CSV.png
    33.2 KB · Views: 5
  • Import_Data_CSV.png
    Import_Data_CSV.png
    55.2 KB · Views: 5

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This is a duplicate of this thread, so is now locked. Please do not post the same question more than once.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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