Alternative to using a lot of If statements. Key/dictionary?

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a list of numbers starting in C1 and ending in in an unknown column of row 1.

Each number is a part ID - for example 33 could be a screw, 414 could be a leg of lamb.

I would like code to check the value of each cell in the row, and replace the number for the 'part' name.

I first thought about doing a If then elseif (for unknowns) but quickly realized the typing this all out would a long time and certainly isn't the best way of doing it for a list of parts that stretches into the hundreds.

Is there a method where I could create some sort of key? or any other suggestions? The list of part numbers and part names can exist either on another sheet (same workbook) - I think this approach might be easiest if I use some sort of Match and offset rule?

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If the part numbers are in Row(1) starting in C1

Put the Part Numbers in say Column(A) of sheet named "Part_Names")

And a script could run replacing the part numbers with Part names.

So In C1 you have "123"
And in sheet named "Part_Names") you have : "Apples" in range("A1") the part number could be replaced with Part name using a Vba script no need for if.
Provide both sheet names.
 
Upvote 0
Let's assume your PartNumbers are in row(1)of sheet named "Part Numbers"
Starting in column "C"
And in sheet named "Part Names" you have the part names in column A Starting in Range("A1")
Have the Part Names in column A in order as they should appear in Row(1) of sheet named PartNumbers
Try using this script.

VBA Code:
Sub Part_Names()
'Modified  12/20/2021  11:09:33 AM  EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim LastColumn As Long
Sheets("Part Numbers").Activate
LastColumn = Sheets("Part Numbers").Cells(1, Columns.Count).End(xlToLeft).Column

    For i = 3 To LastColumn
        Sheets("Part Numbers").Cells(1, i).Value = Sheets("Part Names").Cells(i - 2, 1).Value
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another option.
With a sheet of parts like
+Fluff 1.xlsm
AB
1Part numberPart name
257SE152292
358SJ932469
462SE022984
574TQ414570
676SO902060
794SP931096
8119SX700968
9147SK056479
10194SO994846
11238SE193432
12242SK330786
13421NZ191438
14430SO917942
15477SE101504
16497SE125314
Parts


and data like
+Fluff 1.xlsm
ABCDEFGHIJKLMNOPQR
1119147421497941942385747762430587476242
2
Data


You could use
VBA Code:
Sub Luke()
   Dim Cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("Parts")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Cl.Offset(, 1).Value
      Next Cl
   End With
   With Sheets("Data")
      For Each Cl In .Range("C1", .Cells(1, Columns.Count).End(xlToLeft))
         If Dic.Exists(Cl.Value) Then Cl.Value = Dic(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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