Excel Dynamic Array Make 2 Cols of Data

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,174
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have data below that if there is data in Column C, I need to make a new line so that there are 2 columns worth of data.

From this:
VBA Disipline Make 1 Column with Pri and Sec Sanctions.xlsm
ABC
1SCHOOLIDSANCTION_TYPE_1SANCTION_TYPE_SECONDARY
21502111071
31540111065
415401110201000
51540111090
61540111071
71502111078
81502111070
91540111054
101502111020
111502111070
121540111061
131502111010
141540111070
151540111076
161540111070
171540111076
181540111076
1915401110651000
Sheet2


To This:

VBA Disipline Make 1 Column with Pri and Sec Sanctions.xlsm
GH
1SCHOOLIDSANCTION
21502111071
31540111065
41540111020
51540111000
61540111090
71540111071
81502111078
91502111070
101540111054
111502111020
121502111070
131540111061
141502111010
151540111070
161540111076
171540111070
181540111076
191540111076
201540111065
211540111000
Sheet2
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Is the below what you are after?
Book1
ABCDEFGH
1SCHOOLIDSANCTION_TYPE_1SANCTION_TYPE_SECONDARYSCHOOLIDSANCTION_TYPE
215021110711540111020
315401110651540111065
415401110201000
51540111090
61540111071
71502111078
81502111070
91540111054
101502111020
111502111070
121540111061
131502111010
141540111070
151540111076
161540111070
171540111076
181540111076
1915401110651000
Sheet1
Cell Formulas
RangeFormula
G2:H3G2=FILTER(A2:B19,C2:C19<>"")
Dynamic array formulas.
 
Upvote 0
With VBA:
VBA Code:
Sub Stephen_IV()
Dim i As Long, j As Long, n As Long
Dim va, vb

va = Range("A2", Cells(Rows.Count, "A").End(xlUp)).Resize(, 3)
n = WorksheetFunction.CountA(Range("C:C"))
ReDim vb(1 To UBound(va, 1) + n, 1 To 2)

For i = 1 To UBound(va, 1)
    j = j + 1
    vb(j, 1) = va(i, 1)
    vb(j, 2) = va(i, 2)
    
    If va(i, 3) <> "" Then
            j = j + 1
            vb(j, 1) = va(i, 1)
            vb(j, 2) = va(i, 3)
    End If
Next

Range("G2").Resize(j, 2) = vb
End Sub
 
Upvote 0
if there is data in the SANCTION_TYPE_1 and SANCTION_TYPE_SECONDARY then I would need a new line to handle the SANCTION_TYPE_SECONDARY like below:

If I had
VBA Disipline Make 1 Column with Pri and Sec Sanctions.xlsm
ABC
1SCHOOLIDSANCTION_TYPE_1SANCTION_TYPE_SECONDARY
215401110201000
Sheet3


Then I am looking for this

VBA Disipline Make 1 Column with Pri and Sec Sanctions.xlsm
AB
1SCHOOLIDSANCTION
21540111020
31540111000
Sheet3
 
Upvote 0
How about
Fluff.xlsm
ABCDEF
1SCHOOLIDSANCTION_TYPE_1SANCTION_TYPE_SECONDARY
215021110711502111071
315401110651540111065
4154011102010001540111020
515401110901540111000
615401110711540111090
715021110781540111071
815021110701502111078
915401110541502111070
1015021110201540111054
1115021110701502111020
1215401110611502111070
1315021110101540111061
1415401110701502111010
1515401110761540111070
1615401110701540111076
1715401110761540111070
1815401110761540111076
19154011106510001540111076
201540111065
211540111000
Sheet5
Cell Formulas
RangeFormula
E2:F21E2=HSTACK(TOCOL(IF(B2:C100<>"",A2:A100,1/0),2),TOCOL(B2:C100,1))
Dynamic array formulas.
 
Upvote 0
Thanks, Fluff perfect!!!!! Thank you also Akuini! Much appreciated!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,880
Members
449,477
Latest member
panjongshing

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