Get up to 3 unique values based on two criteria and put in variables in VBA

dwsteyl

New Member
Joined
Sep 28, 2017
Messages
20
Hi guys,

I've searched high and low for a solution to my specific problem, but to no avail. Hence this post.

I am developing an Excel database of drivers and the trucks they drove. These entries are being made via a userform throughout the month as they use the truck. The basic table as captured in Excel looks like this:

Name
JamesJanuaryTruck A
JockFebruaryTruck B
JohnMarchTruck C
KeithJanuaryTruck B
JamesFebruaryTruck A
JockMarchTruck C
JohnJanuaryTruck C
KeithFebruaryTruck B
JohnMarchTruck B
JockJanuaryTruck C
JohnFebruaryTruck C
KeithMarchTruck B
JamesJanuaryTruck C
JockFebruaryTruck A
JohnMarchTruck B

<tbody>
[TD="width: 64"]Month[/TD]
[TD="width: 64"]Truck[/TD]

</tbody>

I then have a userform that will be used for basic reporting (this is where my question originated from). On that userform one can select the driver name in one combobox, and the month in another combobox. After that a button is clicked to load the unique trucks that the given driver drove with during that month.

Any given driver will drive with at most three trucks in any given month, but can use any of those three trucks multiple times.

I therefore need VBA code that will get all the unique trucks a specific driver drove with, and save them in Variable1, Variable2 and Variable3. The data type for this can be string or variant. If a driver only drove with one truck only Variable1 will have data in it. It is fine if the other variables are declared - they will just be NULL value and will not result in anything.

So, for example - if driver John is selected, with month March, the unique trucks driven would be Truck C and Truck B, which can be saved in Variable1 and Variable2 (this will then be transfered to textboxes and used for other functions).

The reason this became complicated for me is that John drove more than once with Truck B (could be true for any other truck or all of them). But since that truck is a unique value I only need to know that the truck was driven by John for that specific month. A driver can therefore use any truck multiple times during any given month.

I hope I got my problem across. Please feel free to ask questions if anything is unclear.

I have tried code for this, but I feel so far from the mark that I am not going to post it.

Thanks in advance!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try something like this...

Code:
    [color=green]'Example driver and month[/color]
    Driver = "John": Mo = "March"
    [color=green]'Read data from sheet[/color]
    Data = Range("A2", Range("C" & Rows.Count).End(xlUp)).Value
    
    [color=darkblue]For[/color] i = [color=darkblue]LBound[/color](Data, 1) [color=darkblue]To[/color] [color=darkblue]UBound[/color](Data, 1)
        [color=darkblue]If[/color] Data(i, 1) = Driver And Data(i, 2) = Mo [color=darkblue]Then[/color]
            [color=darkblue]If[/color] Truck1 = "" [color=darkblue]Then[/color]
                Truck1 = Data(i, 3)
            [color=darkblue]ElseIf[/color] Truck2 = "" And Truck1 <> Data(i, 3) [color=darkblue]Then[/color]
                Truck2 = Data(i, 3)
            [color=darkblue]ElseIf[/color] Truck3 = "" And Truck1 <> Data(i, 3) And Truck2 <> Data(i, 3) [color=darkblue]Then[/color]
                Truck3 = Data(i, 3)
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] i
 
Upvote 0
Maybe this where arr2 will hold the trucks

Code:
Sub drivers()

Dim sh As Worksheet, lr As Long, arr, arr2, drv As String, mon As String, i As Long

Set sh = Sheets("Sheet1")
lr = sh.Range("A" & sh.Rows.Count).End(xlUp).Row
arr = sh.Range("A1:C" & lr)

dvr = "James"
mon = "January"

With CreateObject("Scripting.Dictionary")
    For i = LBound(arr) To UBound(arr)
        If arr(i, 1) = dvr Then
            If arr(i, 2) = mon Then
                If Not IsMissing(arr(i, 3)) Then .Item(arr(i, 3)) = 1
            End If
        End If
    Next
    arr2 = .keys
End With

End Sub
 
Upvote 0
AlphaFrog,

Thank you for the quick response.

The code you gave works just fine. Just one thing I did not mention in my original post. The drivers, month and trucks are part of a bigger database. Driver is in column C, month in Column E and Trucks in Column H.

Would it therefore be possible to change the code to evaluate the criteria over said columns and then assign to the three variables?

Thank you for sharing your expertise!
 
Upvote 0
AlphaFrog,

Thank you for the quick response.

The code you gave works just fine. Just one thing I did not mention in my original post. The drivers, month and trucks are part of a bigger database. Driver is in column C, month in Column E and Trucks in Column H.

Would it therefore be possible to change the code to evaluate the criteria over said columns and then assign to the three variables?

Thank you for sharing your expertise!
 
Upvote 0
Code:
    [color=green]'Example driver and month[/color]
    Driver = "John": Mo = "March"
    [color=green]'Read data from sheet[/color]
    Data = Range("C2", Range("H" & Rows.Count).End(xlUp)).Value
    
    [color=darkblue]For[/color] i = [color=darkblue]LBound[/color](Data, 1) [color=darkblue]To[/color] [color=darkblue]UBound[/color](Data, 1)
        [color=darkblue]If[/color] Data(i, 1) = Driver And Data(i, 3) = Mo [color=darkblue]Then[/color]
            [color=darkblue]If[/color] Truck1 = "" [color=darkblue]Then[/color]
                Truck1 = Data(i, 6)
            [color=darkblue]ElseIf[/color] Truck2 = "" And Truck1 <> Data(i, 6) [color=darkblue]Then[/color]
                Truck2 = Data(i, 6)
            [color=darkblue]ElseIf[/color] Truck3 = "" And Truck1 <> Data(i, 6) And Truck2 <> Data(i, 6) [color=darkblue]Then[/color]
                Truck3 = Data(i, 6)
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] i
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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