vba to repeat a set of items based on list

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hi all

i have an excel sheet that looks like below
1644435502143.png


I would like an excel vba that would repeat column B AND C for each item in column E

My final results should like like below
1644435685813.png


Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Could do it using formulas.

Blank_2_Years (3).xlsx
BCDEFGHI
1APPLES123A0001A0001APPLES123
2ORANGES456A0002A0001ORANGES456
3PEARS789A0003A0001PEARS789
4BANANAS101112A0004A0001BANANAS101112
5A0005A0002APPLES123
6A0006A0002ORANGES456
7A0002PEARS789
8A0002BANANAS101112
9A0003APPLES123
10A0003ORANGES456
11A0003PEARS789
12A0003BANANAS101112
13A0004APPLES123
14A0004ORANGES456
15A0004PEARS789
16A0004BANANAS101112
17A0005APPLES123
18A0005ORANGES456
19A0005PEARS789
20A0005BANANAS101112
21A0006APPLES123
22A0006ORANGES456
23A0006PEARS789
24A0006BANANAS101112
Sheet2
Cell Formulas
RangeFormula
E1:E6E1="A"&TEXT(SEQUENCE(6),"0000")
G1:I24G1=LET(a,B1:B4,c,E1:E6,ra,ROWS(a),rt,ra*ROWS(c),ss,MOD(SEQUENCE(rt)-1,ra)+1,CHOOSE({1,2,3},INDEX(c,INT(SEQUENCE(rt,,,1/ra))),INDEX(a,ss),INDEX(C1:C4,ss)))
Dynamic array formulas.
 
Upvote 0
Could do it using formulas.

Blank_2_Years (3).xlsx
BCDEFGHI
1APPLES123A0001A0001APPLES123
2ORANGES456A0002A0001ORANGES456
3PEARS789A0003A0001PEARS789
4BANANAS101112A0004A0001BANANAS101112
5A0005A0002APPLES123
6A0006A0002ORANGES456
7A0002PEARS789
8A0002BANANAS101112
9A0003APPLES123
10A0003ORANGES456
11A0003PEARS789
12A0003BANANAS101112
13A0004APPLES123
14A0004ORANGES456
15A0004PEARS789
16A0004BANANAS101112
17A0005APPLES123
18A0005ORANGES456
19A0005PEARS789
20A0005BANANAS101112
21A0006APPLES123
22A0006ORANGES456
23A0006PEARS789
24A0006BANANAS101112
Sheet2
Cell Formulas
RangeFormula
E1:E6E1="A"&TEXT(SEQUENCE(6),"0000")
G1:I24G1=LET(a,B1:B4,c,E1:E6,ra,ROWS(a),rt,ra*ROWS(c),ss,MOD(SEQUENCE(rt)-1,ra)+1,CHOOSE({1,2,3},INDEX(c,INT(SEQUENCE(rt,,,1/ra))),INDEX(a,ss),INDEX(C1:C4,ss)))
Dynamic array formulas.
i would rather use vba
 
Upvote 0
Blank_2_Years (3).xlsx
BCDEFGHI
1APPLES123A0001A0001APPLES123
2ORANGES456A0002A0001ORANGES456
3PEARS789A0003A0001PEARS789
4BANANAS101112A0004A0001BANANAS101112
5A0005A0002APPLES123
6A0006A0002ORANGES456
7A0002PEARS789
8A0002BANANAS101112
9A0003APPLES123
10A0003ORANGES456
11A0003PEARS789
12A0003BANANAS101112
13A0004APPLES123
14A0004ORANGES456
15A0004PEARS789
16A0004BANANAS101112
17A0005APPLES123
18A0005ORANGES456
19A0005PEARS789
20A0005BANANAS101112
21A0006APPLES123
22A0006ORANGES456
23A0006PEARS789
24A0006BANANAS101112
Sheet2


VBA Code:
Sub CMBO()
Dim AR1() As Variant:       AR1 = Range("B1:C" & Range("B" & Rows.Count).End(xlUp).Row).Value2
Dim AR2() As Variant:       AR2 = Range("E1:E" & Range("E" & Rows.Count).End(xlUp).Row).Value2
Dim r As Range:             Set r = Range("G1")

With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(AR2)
        For j = 1 To UBound(AR1)
            .Add AR2(i, 1) & ";" & AR1(j, 1) & ";" & AR1(j, 2)
        Next j
    Next i
    Set r = r.Resize(.Count)
    r.Value2 = Application.Transpose(.toArray)
    r.TextToColumns DataType:=xlDelimited, Semicolon:=True
End With

End Sub
 
Upvote 0
Blank_2_Years (3).xlsx
BCDEFGHI
1APPLES123A0001A0001APPLES123
2ORANGES456A0002A0001ORANGES456
3PEARS789A0003A0001PEARS789
4BANANAS101112A0004A0001BANANAS101112
5A0005A0002APPLES123
6A0006A0002ORANGES456
7A0002PEARS789
8A0002BANANAS101112
9A0003APPLES123
10A0003ORANGES456
11A0003PEARS789
12A0003BANANAS101112
13A0004APPLES123
14A0004ORANGES456
15A0004PEARS789
16A0004BANANAS101112
17A0005APPLES123
18A0005ORANGES456
19A0005PEARS789
20A0005BANANAS101112
21A0006APPLES123
22A0006ORANGES456
23A0006PEARS789
24A0006BANANAS101112
Sheet2


VBA Code:
Sub CMBO()
Dim AR1() As Variant:       AR1 = Range("B1:C" & Range("B" & Rows.Count).End(xlUp).Row).Value2
Dim AR2() As Variant:       AR2 = Range("E1:E" & Range("E" & Rows.Count).End(xlUp).Row).Value2
Dim r As Range:             Set r = Range("G1")

With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(AR2)
        For j = 1 To UBound(AR1)
            .Add AR2(i, 1) & ";" & AR1(j, 1) & ";" & AR1(j, 2)
        Next j
    Next i
    Set r = r.Resize(.Count)
    r.Value2 = Application.Transpose(.toArray)
    r.TextToColumns DataType:=xlDelimited, Semicolon:=True
End With

End Sub
Thanks you very much, just one thing if you can add please it doesn't work if there is only one value in column E there needs to be more for it to work
#2 if the value in column E would cause a duplicate in column G it should do nothing, For ex if in column e you have two entries of A01 it should only do it once.

Thanks
 
Upvote 0
How about this?

VBA Code:
Sub CMBO()
Dim r As Range:             Set r = Range("G1")
Dim er As Range:            Set er = Range("E1:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim AR1() As Variant:       AR1 = Range("B1:C" & Range("B" & Rows.Count).End(xlUp).Row).Value2
Dim AR2() As Variant
Dim SD As Object:           Set SD = CreateObject("Scripting.Dictionary")

If er.Cells.Count = 1 Then
    ReDim AR2(1 To 1, 1 To 1)
    AR2(1, 1) = er.Value2
Else
    AR2 = er.Value2
End If

With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(AR2)
        If Not SD.exists(AR2(i, 1)) Then
            SD.Add (AR2(i, 1)), 1
            For j = 1 To UBound(AR1)
                .Add Join(Array(AR2(i, 1), AR1(j, 1), AR1(j, 2)), ";")
            Next j
        End If
    Next i
    Set r = r.Resize(.Count)
    r.Value2 = Application.Transpose(.toArray)
    r.TextToColumns DataType:=xlDelimited, Semicolon:=True
End With

End Sub
 
Upvote 0
How about this?

VBA Code:
Sub CMBO()
Dim r As Range:             Set r = Range("G1")
Dim er As Range:            Set er = Range("E1:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim AR1() As Variant:       AR1 = Range("B1:C" & Range("B" & Rows.Count).End(xlUp).Row).Value2
Dim AR2() As Variant
Dim SD As Object:           Set SD = CreateObject("Scripting.Dictionary")

If er.Cells.Count = 1 Then
    ReDim AR2(1 To 1, 1 To 1)
    AR2(1, 1) = er.Value2
Else
    AR2 = er.Value2
End If

With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(AR2)
        If Not SD.exists(AR2(i, 1)) Then
            SD.Add (AR2(i, 1)), 1
            For j = 1 To UBound(AR1)
                .Add Join(Array(AR2(i, 1), AR1(j, 1), AR1(j, 2)), ";")
            Next j
        End If
    Next i
    Set r = r.Resize(.Count)
    r.Value2 = Application.Transpose(.toArray)
    r.TextToColumns DataType:=xlDelimited, Semicolon:=True
End With

End Sub
Thank you very much that worked
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,577
Members
449,039
Latest member
Arbind kumar

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