VBA to create reference number based on another cell

notruck

New Member
Joined
Jan 19, 2011
Messages
34
Hello All ......

I need to create a "reference" or "sequence" number like seen in the number column below. This number would only be created when "Weekend Pass" is in the type column and would need to loop through all rows in the worksheet. The quantity and type columns are the only columns with existing data. Any ideas?


NumberQuantityType
0011Weekend Pass with Night Access
0021Weekend Pass with Night Access
1Night Access
003, 0042Weekend Pass
0051Weekend Pass with Night Access
006, 0072Weekend Pass
1Monthly
0081Weekend Pass
0091Weekend Pass with Night Access
3Monthly
010, 011, 0123Weekend Pass
1Monthly
1Night Access
1Night Access
0131Weekend Pass with Night Access
0141Weekend Pass

<tbody>
</tbody>
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG23Jul56
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] InStr(Dn.Value, "Weekend Pass") > 0 [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]For[/COLOR] n = 1 To Dn.Offset(, -1)
            c = c + 1
            nstr = nstr & IIf(nstr = "", Format(c, "000"), ", " & Format(c, "000"))
        [COLOR="Navy"]Next[/COLOR] n
    [COLOR="Navy"]With[/COLOR] Dn.Offset(, -2)
        .NumberFormat = "000"
        .Value = nstr: nstr = ""
    [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

notruck

New Member
Joined
Jan 19, 2011
Messages
34
Thanks Mick .... awesome, works great, fast response, terrific. But I forgot to mention something :rolleyes:. Is it possible to use an OR with InStr? I forgot "Exempt" as a type.

Try this:-
Code:
[COLOR=Navy]Sub[/COLOR] MG23Jul56
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] nstr [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Set[/COLOR] Rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
    [COLOR=Navy]If[/COLOR] InStr(Dn.Value, "Weekend Pass") > 0 [COLOR=Navy]Then[/COLOR]
        [COLOR=Navy]For[/COLOR] n = 1 To Dn.Offset(, -1)
            c = c + 1
            nstr = nstr & IIf(nstr = "", Format(c, "000"), ", " & Format(c, "000"))
        [COLOR=Navy]Next[/COLOR] n
    [COLOR=Navy]With[/COLOR] Dn.Offset(, -2)
        .NumberFormat = "000"
        .Value = nstr: nstr = ""
    [COLOR=Navy]End[/COLOR] With
[COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR] Dn
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG23Jul04
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nstr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] InStr(Dn.Value, "Weekend Pass") > 0 Or InStr(Dn.Value, "Exempt") > 0 [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]For[/COLOR] n = 1 To Dn.Offset(, -1)
            c = c + 1
            nstr = nstr & IIf(nstr = "", Format(c, "000"), ", " & Format(c, "000"))
        [COLOR="Navy"]Next[/COLOR] n
    [COLOR="Navy"]With[/COLOR] Dn.Offset(, -2)
        .NumberFormat = "000"
        .Value = nstr: nstr = ""
    [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

Watch MrExcel Video

Forum statistics

Threads
1,101,914
Messages
5,483,688
Members
407,399
Latest member
Rakeforms

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top