VBA: need help matching range values to each other

Lil Stinker

Board Regular
Joined
Feb 16, 2022
Messages
143
Office Version
  1. 2019
Platform
  1. Windows
Need help with VBA to match/search/find values in a range on one sheet with the same values in another range on a second sheet and input data when the match is found.

Sheet1"Receipt" is a form where multiple serial numbers are entered into the Range B10:S90. (Serial numbers are alphanumeric and sometimes start with 0)
Sheet2"Log" is where I have all of the existing serial numbers stored (as text) in Column B.

The task is to loop through every serial number on Sheet1 and match them to Sheet2. Provided there is a match, enter the date found on Sheet1 cell A1 into Column E on Sheet2 of the matching serial number row. If no match, alert user serial number needs to be entered into inventory.

It seems simple enough for an experienced VBA user but I'm not that.

I would appreciate any help you can provide.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi @Lil Stinker
I hope you are well.

There are a couple of things that don't make sense to me.To make it easier to understand your problem.
Could you put some images or a minisheet of what you have on your sheets before the macro?
And what result do you want after the macro?
Please, put several examples 10 or 15.​
And this is not clear to me:
If no match, alert user serial number needs to be entered into inventory.
How do you want the alert?

Explain everything with examples.


--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Upvote 0
Hi Dante,
Thanks for the reply. So, Sheet1 is where we input the serial numbers for equipment that is going out on rental. It can vary from a few items to hundreds so I didn't include the entire range where the numbers are entered because most of the time, the range will have blank cells. The full range would be B10:O90. I will have conditional formatting set up to call out any duplicate entries prior to running the macro otherwise, every serial number is unique.
Book1
ABCDEFGHIJKLMNOP
12/3/2023Date Out2/13/2023Date InLocationOC Carnival Event
2
10SERIAL NUMBERS
11000110009901239089991000100110021234124501890111111245650055
1250555506005065350654506551324465446670047889789897132113571351
131324846513305131213213284683215351330134865679005460056110012346
1410010111212121135454611150150014565646525656445575555066650750
15
16
17
18
19
20
Sheet1


Sheet2 is our inventory sheet where all 2500 serial numbers are stored for each item. What I would like is for the inventory to be updated regularly when the macro is run based on the numbers being logged out on Sheet1. If the number appears on Sheet1, it means on Sheet2, the status should be set to Out, the date out and the location should be recorded from Sheet1 into the corresponding columns on Sheet2. Basically, resembling the last few rows where I've input the data manually.
Book1
ABCDEFGH
1Item IDSerialNumberModelDescriptionStatusLocationDateOutReturnDate
2100011Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
3200099Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
430123Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
54908Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
65999Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
761000Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
871001Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
981002Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
1091234Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
11101245Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
121101890Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
131211111Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
141312456Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
151450055Motorola Radius CP200UHF 16 Channel WalkieINWarehouse
1615213Motorola ChargerMulti Unit ChargerOUTOC Carnival Event2/3/2023
1716305Motorola ChargerMulti Unit ChargerOUTOC Carnival Event2/3/2023
1817250Motorola ChargerMulti Unit ChargerOUTOC Carnival Event2/3/2023
1918832BPower SupplyPower SupplyOUTOC Carnival Event2/3/2023
20191019Power SupplyPower SupplyOUTOC Carnival Event2/3/2023
21201321Power SupplyPower SupplyOUTOC Carnival Event2/3/2023
22211804Power SupplyPower SupplyOUTOC Carnival Event2/3/2023
Sheet2


As for the alert, just a simple MsgBox pop-up alerting the user that the serial number was not found in inventory is all I'm looking for.

Thanks for taking the time!
 
Upvote 0
Hi Lil
Thanks for posting the examples.

Try this macro:
VBA Code:
Sub out_serial_number()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant, c As Variant
  Dim dic As Object, i&, j&
  Dim dt As Date, lo As String
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  Set dic = CreateObject("Scripting.Dictionary")
  
  a = sh1.Range("B11:O" & sh1.Range("B:O").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row).Value
  b = sh2.Range("B2:B" & sh2.Range("B" & Rows.Count).End(3).Row).Value
  c = sh2.Range("E2:G" & sh2.Range("B" & Rows.Count).End(3).Row).Value
  
  dt = sh1.Range("A1").Value
  lo = sh1.Range("H1").Value
  
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then dic(a(i, j)) = Empty
    Next
  Next
  
  For i = 1 To UBound(b, 1)
    If dic.exists(b(i, 1)) Then
      c(i, 1) = "Out"
      c(i, 2) = lo
      c(i, 3) = dt
    End If
  Next
  
  sh2.Range("E2").Resize(UBound(c, 1), UBound(c, 2)).Value = c
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
I forgot the alert message, better try this version:

VBA Code:
Sub out_serial_number_v2()
  Dim rng As Range, c As Range, f As Range
  Dim dt As Date, lo As String, cad As String
  
  With Sheets("Sheet1")
    Set rng = .Range("B11:O90")
    dt = .Range("A1").Value
    lo = .Range("H1").Value
  End With
  
  For Each c In rng
    If c.Value <> "" Then
      With Sheets("Sheet2")
        Set f = .Range("B:B").Find(c.Value, , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          .Range("E" & f.Row).Value = "OUT"
          .Range("F" & f.Row).Value = lo
          .Range("G" & f.Row).Value = dt
        Else
          cad = cad & c.Value & vbCr
        End If
      End With
    End If
  Next
  If cad <> "" Then
    MsgBox cad, , "Serial number was not found in inventory"
  End If
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
Wow! Both worked exceedingly well on my sample data set. I will try plugging in the second version into my workbook tomorrow and see if any issues pop up. I prefer using code I can understand in my limited capacity and I can get an idea of what's happening in version two. Version one is way over my head! Reason being, I may need to manipulate this later on as needs change.

I will get back to you tomorrow with the results. Thank you!
 
Upvote 0
Hello Dante,
The second version works great! I'm still learning VBA so, if you don't mind, what does cad represent?
cad = cad & c.Value & vbCr
If cad <> "" Then MsgBox cad, , "Serial number was not found in inventory"
If I wanted to add more of a message to the MsgBox, how would I go about doing that? For instance, if I wanted the MsgBox Title to say "Not Found" and the body of the MsgBox to say "The following serial numbers were not found in inventory:" followed by the list of serial numbers (or cad)... is this possible?
 
Upvote 0
what does cad represent?
"cad" is just the name of a variable which will store a text.

Try:
VBA Code:
MsgBox "The following serial numbers were not found in inventory:" & vbCr & cad, , "Not Found"
 
Upvote 0
Now for the tricky part. When it comes to returning the equipment to inventory, the form design is the same however, I have a macro set up to change the color of the serial number cells to indicate their returned status. For example, new returns will be colored yellow, damaged returns colored red, previous returns colored gray and serial numbers that are still out have no cell color but the font is gray.

How do I add to or change version 2 (in a new Sub) to update Sheet2 to say "IN" in column E for yellow colored cells on Sheet1, "DMG" in column E for red cells and ignore any gray cells or non-colored cells?

Here's a sample:
Book1.xlsm
ABCDEFGHIJKLMNOP
12/3/2023Date Out2/13/2023Date InLocationOC Carnival Event
2
10SERIAL NUMBERS
113450647065465406541020105816573165323032453251354504657
120465746844987651365406545654665486548656568136878687807898
13798787878987987313151135151651132151345163540835484385414564546578
14468785065478948183950495451050011050120051845625452254536500680000
15550065009465885858
16
17-> previously marked in returns (can be ignored or empty)
18-> new returns marked in (should be marked "IN" on Sheet2)
19-> new return but damaged (should be marked "DMG" on Sheet2)
20-> numbers with no coloring indicate not returned status (can be ignored or empty)
21
Sheet1
 
Upvote 0
Hi @Lil Stinker .
These are new requests, please create a new thread.
There it explains in detail what you need, you put your macro to modify it or in a new macro they help you with what you need now.
;)
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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