Macro/VBA to present user a look up box, and then add rows depending number of rows needed.

cmeredith1973

New Member
Joined
Apr 8, 2019
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi - first time caller, long time listener.

In my world, I have a long list of parts. What I want to do, is create a macro, that will when run, ask me what part # are you looking for.
When found, it looks up the part and inserts the necessary # of rows.

What I have is service kits that are made up of part numbers. The service kits are intermixed with a list of part numbers. So each time a service kit is searched and found, xl will add the necessary part numbers that make up that service kit to explode the parts found within the kit. This will give me a true count of the parts ordered and found.

example
Parts Col.
S81001 is made up of 3 part numbers. When the xl finds the S81001 it adds 3 rows below it. If the xl finds a smaller service kits, it adds 2 rows.
thank you in advance
 
Hi - its the part description and I can pull that in with a look up after I get the parts exploded from kit -> part. It will change as parts get updated.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Ok, try this in small sample first so you can check manually whether the result is correct:
Just change 'Sheets("sheet2")' to 'Sheets("Pairs")'

Code:
[FONT=lucida console][color=Royalblue]Sub[/color] a1110060a()
[i][color=seagreen]'https://www.mrexcel.com/forum/excel-questions/1110060-macro-vba-present-user-look-up-box-then-add-rows-depending-number-rows-needed.html[/color][/i]
[color=Royalblue]Dim[/color] i [color=Royalblue]As[/color] [color=Royalblue]Long[/color], k [color=Royalblue]As[/color] [color=Royalblue]Long[/color], n [color=Royalblue]As[/color] [color=Royalblue]Long[/color]
[color=Royalblue]Dim[/color] va, vb, vc, qx
[color=Royalblue]Dim[/color] d [color=Royalblue]As[/color] [color=Royalblue]Object[/color]
[color=Royalblue]Dim[/color] ws [color=Royalblue]As[/color] Worksheet

Sheets([color=brown]"sheet2"[/color]).Activate  [i][color=seagreen]'change this to suit[/color][/i]
Range([color=brown]"A:A"[/color]).NumberFormat = [color=brown]"@"[/color]

[color=Royalblue]Set[/color] ws = Sheets([color=brown]"Service Kits"[/color])

[color=Royalblue]With[/color] ws
    [color=Royalblue]Set[/color] d = CreateObject([color=brown]"scripting.dictionary"[/color])
    va = .Range([color=brown]"A2:C"[/color] & .Cells(Rows.count, [color=brown]"A"[/color]).[color=Royalblue]End[/color](xlUp).Row)
    [color=Royalblue]For[/color] i = [color=crimson]1[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color])
        d(va(i, [color=crimson]1[/color])) = d(va(i, [color=crimson]1[/color])) + [color=crimson]1[/color]
    [color=Royalblue]Next[/color]
[color=Royalblue]End[/color] [color=Royalblue]With[/color]

vb = Range([color=brown]"A2:C"[/color] & Cells(Rows.count, [color=brown]"A"[/color]).[color=Royalblue]End[/color](xlUp).Row)
[color=Royalblue]ReDim[/color] vc([color=crimson]1[/color] [color=Royalblue]To[/color] [color=crimson]100000[/color], [color=crimson]1[/color] [color=Royalblue]To[/color] [color=crimson]3[/color])
k = [color=crimson]1[/color]

[color=Royalblue]For[/color] i = [color=crimson]1[/color] [color=Royalblue]To[/color] UBound(vb, [color=crimson]1[/color])
    
    qx = vb(i, [color=crimson]1[/color])
    [color=Royalblue]If[/color] d.Exists(qx) [color=Royalblue]Then[/color]
        
        fm = Application.Match(qx, ws.Range([color=brown]"A:A"[/color]), [color=crimson]0[/color])
            [color=Royalblue]For[/color] n = fm - [color=crimson]1[/color] [color=Royalblue]To[/color] fm + d(qx) - [color=crimson]2[/color]
               vc(k, [color=crimson]1[/color]) = va(n, [color=crimson]3[/color])
               [i][color=seagreen]'vc(k, 2) = vb(i, 2) ' empty??[/color][/i]
               vc(k, [color=crimson]3[/color]) = va(n, [color=crimson]2[/color]) * vb(i, [color=crimson]3[/color])
               k = k + [color=crimson]1[/color]
            [color=Royalblue]Next[/color]
            
    [color=Royalblue]Else[/color]
            vc(k, [color=crimson]1[/color]) = [color=Royalblue]CStr[/color](vb(i, [color=crimson]1[/color]))
            vc(k, [color=crimson]2[/color]) = vb(i, [color=crimson]2[/color])
            vc(k, [color=crimson]3[/color]) = vb(i, [color=crimson]3[/color])
            k = k + [color=crimson]1[/color]
    
    [color=Royalblue]End[/color] [color=Royalblue]If[/color]
    
[color=Royalblue]Next[/color]

Range([color=brown]"A2"[/color]).Resize(k, [color=crimson]3[/color]) = vc

[color=Royalblue]End[/color] [color=Royalblue]Sub[/color][/FONT]


Using this example:


Excel 2013
ABC
1PART_IDMISC_REFERENCE
2S81001BURNER, RPLMNT LEX485/605/730,LE,LD4854
3S81001BURNER, RPLMNT LEX485/605/730,LE,LD4851
4S81001BURNER, RPLMNT LEX485/605/730,LE,LD4853
5S81004BURNER, IR SIDE ROGUE 365/425/525/6251
6S81004BURNER, IR SIDE ROGUE 365/425/525/6251
7N190-0001* NGZ PACK , BATTERY LED LIGHTS1
8N475-0399-GY1SGPANEL, REAR CART GREY PRO500-11
9S83007GRIDS, SS 525 SERIES1
10S83007GRIDS, SS 525 SERIES1
Sheet2


the result:


Excel 2013
ABC
1PART_IDMISC_REFERENCE
2N100-00364
3N305-0057-M014
4N570-00084
5N100-00361
6N305-0057-M011
7N570-00081
8N100-00363
9N305-0057-M013
10N570-00083
11N100-00531
12N100-00531
13N190-0001* NGZ PACK , BATTERY LED LIGHTS1
14N475-0399-GY1SGPANEL, REAR CART GREY PRO500-11
15N305-01011
16N305-00992
17N305-01011
18N305-00992
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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