Insert a variable number of rows based on the numerical value of a cell.

jackrobat

New Member
Joined
Jul 15, 2015
Messages
2
Below is an example of a text based spreadsheet we use for cross referencing valid cost codes. I am trying to eliminate the Write-in range of numbers and assign actual rows for each of those numbers so they can be used in a look up table. I added column F as LEFT formula and Column H as a RIGHT Formula to isolate the numbers so I could perform the difference calc in Col H.

Ideally, I'd like to insert the correct number of rows below the first write in and number them accordingly, Big Bonus if I could include the title "Write In" into column C.

I am using Excel 2013
rowb1c1d1e1f1g1h1
2000001180ConsultantWKrate00001180000011800
2100001190 thru 00001199Write inARAR00001190000011999
5000001271Power ConsumptionWKrate00001271000012710
5100001290 thru 00001295Write InARAR00001290000012955

<tbody>
</tbody>
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Jackrobat,
it sounds like your easiest solution would be a macro that does this for you. What is the end result you are looking for? Just a row of account numbers?
Cheers,
Koen
 
Upvote 0
Try this:- for results on sheet2 based on Data in sheet1
Code:
[COLOR="Navy"]Sub[/COLOR] MG20Jul41
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nRw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
Ray = Sheets("Sheet1").Range("A1").CurrentRegion
 ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 1), 1 To UBound(Ray, 2))


[COLOR="Navy"]For[/COLOR] Rw = 2 To UBound(Ray, 1)
    [COLOR="Navy"]If[/COLOR] InStr(Ray(Rw, 2), "thru") > 0 [COLOR="Navy"]Then[/COLOR]
        Sp = Split(Ray(Rw, 2), "thru")
        [COLOR="Navy"]For[/COLOR] nRw = Val(Trim(Sp(0))) To Val(Trim(Sp(UBound(Sp))))
            c = c + 1
            [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(Ray, 2)
                nray(c, Ac) = IIf(Ac = 2, "0000" & nRw, Ray(Rw, Ac))
            [COLOR="Navy"]Next[/COLOR] Ac
        [COLOR="Navy"]Next[/COLOR] nRw
   [COLOR="Navy"]Else[/COLOR]
            c = c + 1
            [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(Ray, 2)
                nray(c, Ac) = Ray(Rw, Ac)
            [COLOR="Navy"]Next[/COLOR] Ac
  [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Rw
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, UBound(Ray, 2))
   .Parent.Range("B:B,F:F,G:G").NumberFormat = "@"
   .Value = nray
   .Columns.AutoFit
   .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:- for results on sheet2 based on Data in sheet1
Code:
[COLOR="Navy"]Sub[/COLOR] MG20Jul41
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] nRw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
Ray = Sheets("Sheet1").Range("A1").CurrentRegion
 ReDim nray(1 To UBound(Ray, 1) * UBound(Ray, 1), 1 To UBound(Ray, 2))


[COLOR="Navy"]For[/COLOR] Rw = 2 To UBound(Ray, 1)
    [COLOR="Navy"]If[/COLOR] InStr(Ray(Rw, 2), "thru") > 0 [COLOR="Navy"]Then[/COLOR]
        Sp = Split(Ray(Rw, 2), "thru")
        [COLOR="Navy"]For[/COLOR] nRw = Val(Trim(Sp(0))) To Val(Trim(Sp(UBound(Sp))))
            c = c + 1
            [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(Ray, 2)
                nray(c, Ac) = IIf(Ac = 2, "0000" & nRw, Ray(Rw, Ac))
            [COLOR="Navy"]Next[/COLOR] Ac
        [COLOR="Navy"]Next[/COLOR] nRw
   [COLOR="Navy"]Else[/COLOR]
            c = c + 1
            [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(Ray, 2)
                nray(c, Ac) = Ray(Rw, Ac)
            [COLOR="Navy"]Next[/COLOR] Ac
  [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Rw
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, UBound(Ray, 2))
   .Parent.Range("B:B,F:F,G:G").NumberFormat = "@"
   .Value = nray
   .Columns.AutoFit
   .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick



Thanks I will give this a try. I ended up writing something that sort of added the rows then used several "if" macros to populate a column but I am finding errors here and there.
I'll try yours out on the original and see if it cleans things up for me.
Thanks again
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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