Return a value if data matches

reseng

New Member
Joined
Jun 24, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have two separate spreadsheets with vehicle registrations and dates. The two spreadsheets correspond to two different locations. The dates correspondence to when a vehicle made a trip to that location.



I want to find out when a particular vehicle made a trip to both locations on same date and return vehicle reg. Like matching dates for a particular vehicle for two locations. Really appreciate your help in advance.
 
I mean what if the recorded dates are not in column B but some random column? Does that warrant a change to script? Cause I tried and if I put the recorded dates next to vehicle reg columns, it works fine but if they are not next column then macro doesn't work.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
If they will always be in a random column, will the header in row one of that column always be the same and if so, what is the header name?
 
Upvote 0
Not necessarily. They can be random but in this instance Sheet1 date header is "Date Gross Weighed" and Sheet2 header is "GD".
 
Upvote 0
The macro can find the column using the header name in row 1 but if the header name keeps changing, then the code has no way to find it.
 
Upvote 0
Ok I will change header so that they are same. What other changes do I need to make? And goes without saying, I really appreciate your help.
 
Upvote 0
Will the vehicle registration always be in column A?
 
Upvote 0
Actually it was in column A in sheet1 but in a different column in sheet2 so I changed it to the appropriate column. But as far as date was in next column it worked.
 
Upvote 0
The macro assumes that the vehicle registration is always in column A of both sheets and that the headers are "Date Gross Weighed" and "GD".
VBA Code:
Sub CompareData()
    Application.ScreenUpdating = False
    Dim WS1 As Worksheet, WS2 As Worksheet, v1 As Variant, v2 As Variant, dic As Object, val As String, hd1 As Range, hd2 As Range
    Set WS1 = ThisWorkbook.Sheets("Sheet1")
    Set WS2 = ThisWorkbook.Sheets("Sheet2")
    Set hd1 = WS1.Rows(1).Find("Date Gross Weighed", LookIn:=xlValues, lookat:=xlWhole)
    Set hd2 = WS2.Rows(1).Find("GD", LookIn:=xlValues, lookat:=xlWhole)
    v1 = WS1.Range("A2", WS1.Range("A" & Rows.Count).End(xlUp)).Resize(, hd1.Column).Value
    v2 = WS2.Range("A2", WS2.Range("A" & Rows.Count).End(xlUp)).Resize(, hd2.Column).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        val = v1(i, 1) & "|" & v1(i, hd1.Column)
        If Not dic.exists(val) Then
            dic.Add val, i + 1
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        val = v2(i, 1) & "|" & v2(i, hd2.Column)
        If dic.exists(val) Then
            With WS1
                .Cells(dic(val), hd1.Column + 1) = v2(i, 1)
            End With
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
The column to the right of the date.
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,117
Members
451,743
Latest member
matt3388

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