INSRWS

INSRWS(a,[r],[g])
a
array
[r]
row's group size, if omitted r=1
[g]
gap size, if omitted g=1, if<0 gap inserted also before 1st row

Inserts one or more blank rows

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
860
Office Version
  1. 365
Platform
  1. Windows
INSRWS Inserts blank Rows. Calls ZINS
- can insert 1 or more blank rows, called "gap", after each group of r rows. Gap size argument g
- if g<0 a nr. of abs(g) blank rows is also inserted before 1st row
Note: Could have embedded ZINS function with INSRWS but can be a useful function tool to solve other types of tasks, like inserting text patterns.
Excel Formula:
=LAMBDA(a, [r], [g],
    LET(
        x, MAX(1, r),
        y, IF(g, ABS(g), 1),
        q, ZINS(ROWS(a), x, y),
        s, IF(g < 0, VSTACK(SEQUENCE(y) ^ 0 - 1, q), q),
        b, INDEX(IF(a = "", "", a), s, SEQUENCE(, COLUMNS(a))),
        IF(s, b, "")
    )
)
ZINS(n,x,g) Zeros Insert, Inserts 1 or more zeros to a sequence of n elements
n: nr. of elements
x: element's group size
g: 0's group size
Excel Formula:
=LAMBDA(n, x, g,
    LET(
        d, IF(SEQUENCE(x) < x, ",", "," & REPT("0,", g)),
        --TEXTSPLIT(TEXTJOIN(d, , SEQUENCE(n)), , ",")
    )
)
Book3
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
1
2r,g,omit.r,2, g,omit.r,omit. ,g,2r,3,g,2r,5,g,3r,omit. ,g,-1r,4,g,-3
3a=INSRWS(B4#)=INSRWS(B4#,2)=INSRWS(B4#,,2)=INSRWS(B4#,3,2)=INSRWS(B4#,5,3)=INSRWS(B4#,,-1)=INSRWS(B4#,4,-3)
4123123123123123123  
5456456456456123
6789456789789
7101112789456101112456123
8131415789101112131415456
9161718101112789789
10192021101112131415789131415101112
11222324161718161718101112
12252627131415161718
13282930192021101112192021131415
14313233161718222324192021222324131415
15343536222324252627161718161718
16373839192021252627131415252627282930192021
17404142282930192021222324
18434445222324
19464748313233161718282930222324
20495051252627343536313233313233
21343536343536252627252627
22282930373839192021373839282930
23404142404142282930313233
24313233373839434445343536
25434445222324404142313233
26343536464748434445
27343536
28373839495051252627464748373839
29464748495051373839404142
30404142495051434445
31282930404142464748
32434445
33434445
34464748313233
35464748495051
36495051
37343536495051
38
39
40373839
41
42
43404142
44
45
46434445
47
48
49464748
50
51
52495051
53
Sheet1
Cell Formulas
RangeFormula
F3,J3,N3,R3,V3,Z3,AD3F3=FORMULATEXT(F4)
B4:D20B4=SEQUENCE(17,3)
F4:H36F4=INSRWS(B4#)
J4:L28J4=INSRWS(B4#,2)
N4:P52N4=INSRWS(B4#,,2)
R4:T30R4=INSRWS(B4#,3,2)
V4:X29V4=INSRWS(B4#,5,3)
Z4:AB37Z4=INSRWS(B4#,,-1)
AD4:AF35AD4=INSRWS(B4#,4,-3)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B4:AF53Expression=B4<>""textNO
B4:AF52Expression="b4<>"""""textNO
B4:AF52Expression="B4<>"""""textNO
 
Upvote 0
STKCTY(tb)
Excel Formula:
=LAMBDA(tb,
    LET(
        c, TAKE(tb, 1),
        cy, FILTER(c, c <> ""),
        k, ASTACK(DROP(tb, 2), 2),
        f, TAKE(k, , 1),
        i, QUOTIENT(SEQUENCE(ROWS(k)) - 1, ROWS(tb) - 2) + 1,
        h, HSTACK(INDEX(cy, i), k),
        l, FILTER(h, f <> ""),
        VSTACK({"City", "Name", "Age"}, l)
    )
)
Book3.xlsx
ABCDEFGHIJKLMNOPQRSTU
1
2MumbaiNew YorkHong KongLondonMumbaiNew YorkHong KongLondonMadrid
3NameAgeNameAgeNameAgeNameAgeNameAgeNameAgeNameAgeNameAgeNameAge
4A14E14I14N5A14E14I14N5R23
5B12F12J12O5B12F12J12O5S32
6c18G18K18p5c18G18K18p5T43
7D10L19Q5D10L19Q5U18
8M20M20V12
9MA17X23
10MB20
11
12=STKCTY(B2:I8)=STKCTY(K2:T10)
13CityNameAgeCityNameAge
14MumbaiA14MumbaiA14
15MumbaiB12MumbaiB12
16Mumbaic18Mumbaic18
17MumbaiD10MumbaiD10
18New YorkE14New YorkE14
19New YorkF12New YorkF12
20New YorkG18New YorkG18
21Hong KongI14Hong KongI14
22Hong KongJ12Hong KongJ12
23Hong KongK18Hong KongK18
24Hong KongL19Hong KongL19
25Hong KongM20Hong KongM20
26LondonN5Hong KongMA17
27LondonO5Hong KongMB20
28Londonp5LondonN5
29LondonQ5LondonO5
30Londonp5
31LondonQ5
32MadridR23
33MadridS32
34MadridT43
35MadridU18
36MadridV12
37MadridX23
38
Sheet19
Cell Formulas
RangeFormula
B12,F12B12=FORMULATEXT(B13)
B13:D29B13=STKCTY(B2:I8)
F13:H37F13=STKCTY(K2:T10)
Dynamic array formulas.
 
Amazing!
I wouldn't have thought of using the QUOTIENT function in that way to get the index values. (I'd have probably used REPT like you did in the your 2nd to last version)
The ASTACK function also really helps.
I guess you can make this generic and fully dynamic to any kind of unpivot and stack operation, for a table of repeating pattern of columns of any width, and pass in the "City" text value as a parameter too.
Thanks again for solving this challenge!
 

Forum statistics

Threads
1,223,575
Messages
6,173,148
Members
452,502
Latest member
PQCurious

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