VBA loop through and copy data to another table

cknnugget

Board Regular
Joined
Jun 29, 2020
Messages
50
Office Version
  1. 365
Platform
  1. Windows
Trying to create a macro to loop through Table 1.
If there is a date in the Table 1 ETA column.
Then Find the matching unique ID in Table 2 and copy the date into the Table 2 ETA column

The solution is probably simple everything I have tried has not come close though.

Table 1
Unique IDETA
1​
12/1/2022​
2​
Table 2
Unique IDETAArrival Date
1​
Filler
2​
Filler
3​
Filler
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Finally figured it out. It is a little laggy but functional

Sub datetest()
Dim ETA As ListObject
Dim DB As ListObject
Set ETA = Sheets(Sheet2.Name).ListObjects("ETA")
Set DB = Sheets(Sheet2.Name).ListObjects("DB")

'Loops through ETA (Table1)to find rows with Date
For i = 1 To ETA.DataBodyRange.Rows.Count
If IsDate(ETA.DataBodyRange(i, 2)) Then

'Loops through DB (Table2) matches the unique ID
For i2 = 1 To DB.DataBodyRange.Rows.Count
If ETA.DataBodyRange(i, 1) = DB.DataBodyRange(i2, 1) And IsEmpty(DB.DataBodyRange(i2, 2)) Then

'copies data from ETA to DB
DB.DataBodyRange(i2, 2) = ETA.DataBodyRange(i, 2)
DB.DataBodyRange(i2, 4) = ETA.DataBodyRange(i, 4)
Else

End If

Next
End If
Next

End Sub
 
Upvote 0
This should have very little "lag" if you want to give it a try:
VBA Code:
Sub DateTestDict()
    Dim tblETA As ListObject
    Dim tblDB As ListObject
    Dim arrETA As Variant
    Dim arrDB As Variant
    Dim i As Long, i2 As Long
    Dim dictETA As Object, dictKey As String
    
    Set tblETA = Range("ETA").ListObject
    Set tblDB = Range("DB").ListObject
    
    arrETA = tblETA.DataBodyRange.Value2
    arrDB = tblDB.DataBodyRange.Value2

    Set dictETA = CreateObject("Scripting.dictionary")
    
    ' Load details range into Dictionary - if ETA is not blank
    For i = 1 To UBound(arrETA)
        If arrETA(i, 2) <> "" Then
            dictKey = arrETA(i, 1)
            If Not dictETA.exists(dictKey) Then
                dictETA(dictKey) = i
            End If
            End If
    Next i
    
    ' Get values from using the dictionary where table DB column ETA is blank
    For i = 1 To UBound(arrDB)
        dictKey = arrDB(i, 1)
        If dictETA.exists(dictKey) Then
            If arrDB(i, 2) = "" Then
                'copies data from ETA to DB
                arrDB(i, 2) = arrETA(dictETA(dictKey), 2)
                arrDB(i, 4) = arrETA(dictETA(dictKey), 4)
            End If
        End If
    Next i
    
    ' Write back updated data
    tblDB.ListColumns(2).DataBodyRange = Application.Index(arrDB, 0, 2)
    tblDB.ListColumns(4).DataBodyRange = Application.Index(arrDB, 0, 4)

End Sub
 
Upvote 0
Solution
The speed difference is huge!
I speed tested both Macros on transferring 100 dates.
My code: 17 seconds
Your code: 0.08 seconds
Thank you for the assistance.
Not sure how the code works. Still new to VBA. Looks like scripting.dictionary and ubound are on the list to learn.
 
Upvote 0
Thanks for giving such specific feedback, it is always nice to know how it performed on the actual data.
When you are ready the terms to look for are Dictionary and Array. Ubound is just getting the upper index number of what you have put into an array, roughly the equivalent of Rows.Count for the number of rows in a range.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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