Match and Modify serial number data using VBA

Carlit007

New Member
Joined
Sep 5, 2018
Messages
47
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
  2. MacOS
Hi I am trying to find out if there's a way to do the following using VBA

I have a workbook with data from a report like the one below consisting of equipment serial numbers
The Items Highlighted in blue are Serial numbers for equipment I have in different locations

EXCEL SCREENSHOT  1.JPG


worksheet 2 has the Serial numbers I constantly track on Colum A, followed by the location on Colum J
LOCATION DATA.JPG


what I would like is for the data imported from a new report in worksheet 1 to find and match the serial number by looking at worksheet 2 and basically adding the location information from colum j right next to the Serial number all in worksheet 1 I have done this in the past by doing vlookup but it is very time consuming as I have to paste the formula right next to an empty cell next to the Serials numbers. what is the best way to go about this thanks in advance
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Update: So I found the following code which sort of takes care of what I was triying to do
__________________________________
Sub Multi_FindReplace()
'PURPOSE: Find & Replace a list of text/values throughout entire workbook from a table
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault


Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant

'Create variable to point to your table
Set tbl = Worksheets("Sheet1").ListObjects("Table2")

'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)

'Designate Columns for Find/Replace data
fndList = 1
rplcList = 5

'Loop through each item in Array lists
For x = LBound(myArray, 1) To UBound(myArray, 2)
'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
For Each sht In ActiveWorkbook.Worksheets
If sht.Name <> tbl.Parent.Name Then

sht.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False

End If
Next sht
Next x

End Sub
________________________________
is there any way that I can format the results to be bold letter & colored blue
 
Upvote 0
Hi @Carlit007 this is a variation on that which I was doing just prior to your follow up post. It includes the Blue and Bold. It presumes that the Numbers and Locations are in the ranges A2:A8/D2:D8 and that the sheets are named Sheet1 and Sheet2. I have also added " Location:" because if you want to strip these hardcoded locations at some point that may make it easier to do so.

VBA Code:
Sub subAddLocations()
    Dim aKeys As Variant
    aKeys = Sheet2.Range("A2:A8").Value
    Dim aItems As Variant
    aItems = Sheet2.Range("D2:D8").Value
    Dim dicObjects As New Scripting.Dictionary
    Dim i As Integer
    For i = 1 To UBound(aKeys)
        dicObjects.Add aKeys(i, 1), aItems(i, 1)
    Next
    Sheet1.Select
    Dim dicKey As Variant
    For Each dicKey In dicObjects
        With Application.ReplaceFormat.Font
            .FontStyle = "Bold"
            .Color = vbBlue
        End With
        Cells.Replace What:=dicKey, Replacement:=dicKey & " Location:" & dicObjects(dicKey), _
        LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=True
    Next
End Sub

Tested on the following:
Book1
ABCD
1Serial NumberxxLOCATION INFO
24THGPL2 xxNGMANB301E80013 (SJ) RM 211 (DESK 2)
3700GPL2xxNGMANB301E80039 (SJ) RM 211 ()
4730GPLxxNGMANB301E80012 (S3) RM 211 (DESK 5)
5J20GPL2xxNGMANB301E80007 (S3) RM 211 ()
6J9Y18M2xxNGMAWK301800012 (S3) RM 211 0
7HHKGPL2xx(NGMAN8301800043 HHC RM 111 (SUPPLY) CLOSET (CMDR OFFICE NEEDS TO GO TO HELPDESK)
88TBGPL2xxNGMAN8301800044 HHC RM 111 (SUPPLY) (CMDR OFFICE NEEDS TO BE AAGN)
Sheet2

Input:
Book1
ABCDEFG
1
2
3J20GPL2
4J9Y18M2
54THGPL2
6
7700GPL2
8730GPL
9
10HHKGPL2
11
128TBGPL2
Sheet1

Output
1582698272578.png
 
Upvote 0
Or an even simpler version without using the dictionary:
VBA Code:
Sub subAddLocations()
    Dim aKeys As Variant
    aKeys = Sheet2.Range("A2:A8").Value
    Dim aItems As Variant
    aItems = Sheet2.Range("D2:D8").Value
    Dim i As Integer
    Sheet1.Select
    With Application.ReplaceFormat.Font
        .FontStyle = "Bold"
        .Color = vbBlue
    End With
    For i = 1 To UBound(aKeys)
        Cells.Replace What:=aKeys(i, 1), Replacement:=aKeys(i, 1) & " Location:" & aItems(i, 1), _
            LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=True
    Next
End Sub
 
Upvote 0
Thank you so much!! @kennypete this worked so much better then my previous code above I was wondering if there's any way to make it so that only the location data is bold and blue leaving the actual serial unaffected by the formatting thanks again
 
Upvote 0
Here's a modified version that will leave the No alone but make the Location blue+bold:

VBA Code:
Sub subAddLocationsV3()
    Dim aKeys As Variant
    aKeys = Sheet2.Range("A2:A8").Value
    Dim aItems As Variant
    aItems = Sheet2.Range("D2:D8").Value
    Dim i As Integer
    Sheet1.Activate
    Application.FindFormat.Clear
    Application.ReplaceFormat.Clear
    For i = 1 To UBound(aKeys)
        Cells.Replace What:=aKeys(i, 1), Replacement:=aKeys(i, 1) & " " & aItems(i, 1), _
            LookAt:=xlWhole, SearchOrder:=xlByRows
    Next
    Dim fAddress As String, sAddress As String, c As Range
    For i = 1 To UBound(aKeys)
        With Sheet1.UsedRange
            Set c = .Find(aKeys(i, 1), LookIn:=xlValues, LookAt:=xlPart)
            If Not c Is Nothing Then
                fAddress = c.Address
                Do
                    sAddress = c.Address
                    Range(sAddress).Activate
                    With ActiveCell.Characters(Len(aKeys(i, 1)) + 1, Len(Range(sAddress).Value)).Font
                        .FontStyle = "Bold"
                        .Color = vbBlue
                    End With
                    Set c = .FindNext(c)
                Loop While Not c.Address = fAddress
            End If
        End With
    Next i
End Sub
Output now...
1582794406095.png
 
Last edited:
Upvote 0
wow this worked out perfectly ,thank you so much! @kennypete I was able to somewhat follow the previous VBA code from V2
what VBA methods do I need to study/learn to figure out everything going on V3 I am intrigued. Thanks again fort your time
 
Upvote 0
All good @Carlit007, glad it worked well for you. I've commented the code in a discursive and not too technical manner, which may help understanding it.

VBA Code:
Sub subAddLocationsV3()
    ' The Serial Numbers are unique primary keys. In this case, because there's
    ' a known fixed number of them, I've used an array to store those keys.
    ' A nicer, and in many cases better, way is to use a dictionary, but in this
    ' use case using two arrays is fine.
    Dim aKeys As Variant
    aKeys = Sheet2.Range("A2:A8").Value
    ' The corresponding items are the locations. For each of the Serial Number keys
    ' store the locations in the same position in another array, aItems.
    Dim aItems As Variant
    aItems = Sheet2.Range("D2:D8").Value
    ' Declaring "i" as an integer for doing the loops. Incidentally, the Long data
    ' type is probably the "fastest" to process now but for understanding,
    ' integer is good and the loop will always be one, so may as well use that
    ' Another incidental point: I have declared all of the variables - you may see
    ' this omitted, bit if it is then they will be Variant data types. It's better
    ' practice to Dim and use precise data types.
    Dim i As Integer
    ' Literally activating Sheet1 because I then do a Search and Replace,
    ' so we need to be in that Worksheet
    Sheet1.Activate
    ' In case you have leftover formatting in the Replace dialog box from previous
    ' searches, this clears it
    Application.FindFormat.Clear
    Application.ReplaceFormat.Clear
    ' Loop from 1 to the number of (i.e. count of) the aKeys array. This is because
    ' we need to replace for every Serial Number
    For i = 1 To UBound(aKeys)
        ' Search for the Serial number. Because of the way the array aKeys is created it
        ' creates a 2D array:
        ' e.g. 4THGPL2 is in aKeys(1, 1), 700GPL2 in aKeys (2, 1) and so on. The find text
        ' is the key, e.g. aKeys(1, 1) / 700GPL2, the replace text is that plus a space
        ' plus the item from the Location array, so, e.g. aItems(1, 1)  / NGMANB301E80013
        ' (SJ) RM 211 (DESK 2). You also need to ensure that the whole cell is searched
        ' because in your example screenshot that is how they appeared.
        Cells.Replace What:=aKeys(i, 1), Replacement:=aKeys(i, 1) & " " & aItems(i, 1), _
            LookAt:=xlWhole, SearchOrder:=xlByRows
    Next
    ' The next step is to loop through the used range of Sheet 1 and apply the
    ' formatting. Three variables are used: one for the cell address (e.g. $B$5) where the
    ' Serial Number being looked for appears, one for the address as a string, and
    ' the third, c, a range (i.e. in this case used for a cell address)
    Dim fAddress As String, sAddress As String, c As Range
    ' Loop (similar to above)
    For i = 1 To UBound(aKeys)
        ' Limits the work to the used range of Sheet1
        With Sheet1.UsedRange
            ' Find the aKeys(i, 1), so for aKeys(1, 1) that's 4THGPL2, in
            ' any values of the cells in the used range and, because those cells
            ' now have the location data also included within them, the search needs
            ' to be for where it appears in *part* of the cell
            Set c = .Find(aKeys(i, 1), LookIn:=xlValues, LookAt:=xlPart)
            ' If there is a cell with the aKeys(i, 1) then set the address
            ' to the address of that cell
            If Not c Is Nothing Then
                fAddress = c.Address
                Do
                    ' Set that address to the string variable sAddress so that
                    ' it can be passed to the "With" that follows
                    sAddress = c.Address
                    ' Literally activate that found cell because we are now wanting
                    ' to apply formatting to an active cell. Normally you don't need
                    ' to select/activate cells, though I'm unsure whether there's any
                    ' way not to in this case of applying *partial* cell formatting.
                    Range(sAddress).Activate
                    ' Apply only Bold+Blue, noting that VBA has a constant vbBlue for
                    ' blue, but this could be a number also representing any "blue"
                    ' colour if the 'default' one is not wanted. Apply it only after the
                    ' text where the Serial Number is appearing, so FROM Len(gth) of aKeys(i, 1)
                    ' plus 1 for the space character that was added TO the end (i.e. the
                    ' Len(gth) of the entire range, which is the cell located at sAddress)
                    With ActiveCell.Characters(Len(aKeys(i, 1)) + 1, Len(Range(sAddress).Value)).Font
                        .FontStyle = "Bold"
                        .Color = vbBlue
                    End With
                    ' Now loop to see whether there are any more of that Serial Number
                    Set c = .FindNext(c)
                ' And if there are do this again, otherwise we've found all of the instances of that Serial Number
                ' in that range.
                Loop While Not c.Address = fAddress
            End If
        End With
    ' Do the next Serial Number
    Next i
End Sub
 
Upvote 0
much appreciation, and many thanks this really helps to speed up the learning by couple of Years ? you sir are a VBA Hero
 
Upvote 0
I have one small question for the following line in the code

Sheet1.Activate 'how do I change this to use the current active sheet in the workbook instead of one named "Sheet1" I understand "sheet2" holds all the range information containing the Serial and location so it doesnt need to change much

I plan on importing numerous worksheets on a frequent basis to run this macro was just wondering if there's any way to automate this without having to change that line everytime to a diferent sheet name
hope that makes sense
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,306
Members
449,218
Latest member
Excel Master

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