VBA - Searching and Returning IF Multiple Query has been met

Razor_Rob

Board Regular
Joined
Aug 18, 2022
Messages
63
Office Version
  1. 365
Platform
  1. Windows
Hi I've got a VBA code that uses the Dictionary Object
wsAvailabilityCodes - worksheet where it search for certain records
wsDest - destination worksheet where the data gets copied into

wsAvailabilityCodes has multiple columns (table below has limited number of columns)

ABCDI....KPQR
1234CPC123456Product 1New YorkUserOfficeG2
5678UEE123456Product 2Los AngelesUserOfficeG2
9112MEM12345-TESTProduct 3DenverStandardWorkG6

My VBA below looks for the value in Column B then returns the value in Column A, I & K.
Now what I want to add is add multiple query like if value of B and = "User" from P and also = "Office" from Q and also = "G2" from R
then return the Value of A, I and K to wsDest
Thanks in advance

VBA Code:
    Dim Col2 As Range
    Dim Dict1 As Object
    ' Dim Dict2 As Object  <-- has been removed (not required)
    Dim Col3 As Range
    Dim Dict3 As Object
    Dim Dict4 As Object
        
    Set Dict1 = CreateObject("scripting.dictionary")
    'Set Dict2 = CreateObject("scripting.dictionary")
    Set Dict3 = CreateObject("scripting.dictionary")
    Set Dict4 = CreateObject("scripting.dictionary")
                
    With wsAvailabilityCodes
        For Each Col2 In .Range("B5", .Range("B" & Rows.Count).End(xlUp))
            Dict1(Col2.Value) = Col2.Value            
            Dict3(Col2.Value) = Col2.Offset(, 7).Value
            Dict4(Col2.Value) = Col2.Offset(, 8).Value
        Next Col2
    End With
    With wsDest
        For Each Col2 In .Range("Y3", .Range("Y" & Rows.Count).End(xlUp))
            If Dict1.exists(Col2.Value) Then Col2.Offset(, 1).Value = Dict1(Col2.Value)            
            If Dict3.exists(Col2.Value) Then Col2.Offset(, 2).Value = Dict3(Col2.Value)
            If Dict4.exists(Col2.Value) Then Col2.Offset(, 4).Value = Dict4(Col2.Value)
        Next Col2
    End With
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You could put an example of what you have in the "wsDest" sheet and put in yellow what you want as a result. Use XL2BB tool to put examples.
 
Upvote 0
Sorry my PC wont allow me to download XL2BB

wsDest comparing Column Y data from wsAvailabilityCodes if the value is the same as column B and if value of B and = "User" from P and also = "Office" from Q and also = "G2" from R
then return the Value of A, I and K to wsDest

values copied over
wsAvailabilityCodeswsDest
comparing Bto the value thats already in column Y
IAC
KAA
AAZ

wsDest Data after
YZAAABAC...........AZ
CPC123456Value from Column K in wsAvailabilityCodes
Value from Column I in wsAvailabilityCodes
Value from Column A in wsAvailabilityCodes
UEE123456122886
MEM12345-TEST611234
 
Last edited:
Upvote 0
See if this works for you.

VBA Code:
Sub ModifiedCode()

    Dim Col2 As Range
    Dim Dict1 As Object
    ' Dim Dict2 As Object  <-- has been removed (not required)
    Dim Col3 As Range
    Dim Dict3 As Object
    Dim Dict4 As Object
    
    Dim wsAvailabilityCodes As Worksheet, wsDest As Worksheet
    Dim lrowAvail As Long, i As Long
    Dim arrAvail As Variant
    Set wsAvailabilityCodes = Worksheets("Availability")
    With wsAvailabilityCodes
        lrowAvail = .Range("B" & Rows.Count).End(xlUp).Row
        arrAvail = .Range("A5:J" & lrowAvail)
    End With
    
    Set wsDest = Worksheets("Destination")
            
    Set Dict1 = CreateObject("scripting.dictionary")
    For i = 1 To UBound(arrAvail)
        Dict1(arrAvail(i, 2)) = i
    Next i
                
    With wsDest
        For Each Col2 In .Range("Y3", .Range("Y" & Rows.Count).End(xlUp))
            If Dict1.exists(Col2.Value) Then Col2.Offset(, 1).Value = arrAvail(Dict1(Col2.Value), 1)
            If Dict1.exists(Col2.Value) Then Col2.Offset(, 2).Value = arrAvail(Dict1(Col2.Value), 9)
            If Dict1.exists(Col2.Value) Then Col2.Offset(, 4).Value = arrAvail(Dict1(Col2.Value), 10)
        Next Col2
    End With

End Sub
 
Upvote 0
if the value is the same as column B and if value of B and = "User" from P and also = "Office" from Q and also = "G2" from R
then return the Value of A, I and K to wsDest

Try this.
Adjust the name of your sheets.

VBA Code:
Sub Searching_Multiple_Query()
  Dim wsCode As Worksheet, wsDest As Worksheet
  Dim dic As Object
  Dim a As Variant
  Dim i As Long
  Dim c As Range
  
  Set dic = CreateObject("Scripting.Dictionary")
  Set wsCode = Sheets("Sheet1")   'wsAvailabilityCodes sheet
  Set wsDest = Sheets("Sheet2")   'destination sheet
  
  a = wsCode.Range("A5:R" & wsCode.Range("B" & Rows.Count).End(3).Row).Value
  For i = 1 To UBound(a, 1)
    If a(i, 16) = "User" And a(i, 17) = "Office" And a(i, 18) = "G2" Then
      'Add to the dictionary only those that meet the conditions
      dic(a(i, 2)) = a(i, 1) & "|" & a(i, 9) & "|" & a(i, 11)
    End If
  Next
  
  For Each c In wsDest.Range("Y3", wsDest.Range("Y" & Rows.Count).End(3))
    If dic.exists(c.Value) Then
      wsDest.Range("AA" & c.Row).Value = Split(dic(c.Value), "|")(2)
      wsDest.Range("AC" & c.Row).Value = Split(dic(c.Value), "|")(1)
      wsDest.Range("AZ" & c.Row).Value = Split(dic(c.Value), "|")(0)
    End If
  Next
End Sub
 
Upvote 0
Hi, seems to be working, will have to do more test
Thank you

Can I add something to the query please
How do I search a partial match in wsAvailabilityCodes column B
example ABC12345-Sample in Column B
and from wsDest column Y the value trying to match is ABC12345
wsAvailabilityCodes column BwsDest column Y
ABC12345-SampleABC12345
EEE30015-DashEEE30015


Also out of topic if I have one of the workbook open already from the VBA code which has a line to open the VBA code errors - Run-time error '91' Object variable or With block variable not set
then highlights the code

Set wsAvailabilityCodes = wbAvailabilityCodes.Worksheets(1)

if the workboook is not open the code runs fine
 
Upvote 0
Hi, seems to be working, will have to do more test
I did the relevant tests to make it work.
But it is up to you to carry out all the tests you consider.

-----
How do I search a partial match in wsAvailabilityCodes column B
With the use of Dictionary it is not possible. I made the macro based on the fact that in your OP you are putting a dictionary, if you want partial searches, we must use another comparison option.
It's something you should mention in your OP.


Try this:
VBA Code:
Sub Searching_Partial()
  Dim wsCode As Worksheet, wsDest As Worksheet
  Dim c As Range, f As Range
  
  Set wsCode = Sheets("Sheet1")   'wsAvailabilityCodes sheet
  Set wsDest = Sheets("Sheet2")   'destination sheet
  
  For Each c In wsDest.Range("Y3", wsDest.Range("Y" & Rows.Count).End(3))
    Set f = wsCode.Range("B:B").Find(c.Value & "*", , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      If wsCode.Range("P" & f.Row).Value = "User" And _
         wsCode.Range("Q" & f.Row).Value = "Office" And _
         wsCode.Range("R" & f.Row).Value = "G2" Then
        wsDest.Range("AA" & c.Row).Value = wsCode.Range("K" & f.Row).Value
        wsDest.Range("AC" & c.Row).Value = wsCode.Range("I" & f.Row).Value
        wsDest.Range("AZ" & c.Row).Value = wsCode.Range("A" & f.Row).Value
      End If
    End If
  Next
End Sub

There are other ways to search partially, but try the one above.
 
Upvote 0
Hi

I tried the code above and there are no data in Columns AA and AC
 
Upvote 0
Before we progress too far down a different Garden path. Can we rely on the format Code+"-"+Text.
If we trim off everything from the "-" is trimmed code from column B in the Availability sheet, still unique and a match for Destination Column Y ?
If not how do we know which one to pick up ?
 
Upvote 0
Its not unique as it will have multiple lines like below, depending on P Q R and now I just realised AA as well. SORRY

wsAvailabilityCodes
ABIKPQRAA
3711ABC12345-Sample51UserOfficeG2502
3711ABC12345-Sample12UserOfficeG2504
3711ABC12345-Sample23UserWorkG6504
3711ABC12345-Sample11UserWorkG2502
3711ABC12345-Sample41CompanyOfficeG2505

wsDest should have
YAAABACAZ
ABC12345150253711
ABC12345250413711

Column AB has been pre populated from copying data before this code.
 
Upvote 0

Forum statistics

Threads
1,215,339
Messages
6,124,363
Members
449,155
Latest member
ravioli44

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