Slicing A Dynamic Array

Beryo

New Member
Joined
Jan 9, 2011
Messages
13
Dear all,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Inputs <o:p></o:p>
1) A single row array – specified by the maximum value (below=10) and generated by randomizing the integers between 1 to maximum value.<o:p></o:p>
2) Number of zeroes to be inserted <o:p></o:p>
e.g. After inserting the zeroes randomly I get the following array <o:p></o:p>
A= [1 8 4 0 6 9 2 7 0 3 5 10]<o:p></o:p>
I would like to dissect A into smaller single row matrices based on the zeroes in the locations <o:p></o:p>
In the example above number of zeroes =2 Hence the matrix needs to be dissected into 3 as follows <o:p></o:p>
B1= [1 8 4]<o:p></o:p>
B2= [6 9 2 7]<o:p></o:p>
B3= [3 5 4]<o:p></o:p>
How can I generate B1, B2, etc based on the number of zeroes?<o:p></o:p>
Once B1,B2,..are created, how can I generate a new matrix C (4X3 matrix in this case) from them?<o:p></o:p>
1 8 4 0 <o:p></o:p>
6 9 2 7<o:p></o:p>
3 5 4 0<o:p></o:p>
Note that C (1, 4) = C(3,4)=0 as the column size of C is limited by B2 (that has 4 elements)<o:p></o:p>
As mentioned the only inputs are the max value and the number of zeroes<o:p></o:p>
Thank you in advance.<o:p></o:p>
B.<o:p></o:p>
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
sheet1 contains data in row no. 1 like this

Excel Workbook
ABCDEFGHIJKL
11840692703510
Sheet1


then park all the three macros in the mdoule. but run ONLY MACRO TEST.

Code:
Sub test()
Dim r As Range, r0 As Range, dest As Range, cfind As Range, add As String
With Worksheets("sheet1")
Set r = .Range("A1")
Set r0 = .Rows("1:1").Cells.Find(what:=0, lookat:=xlWhole, after:=r)
'MsgBox r0.Address
add = r0.Address
Range(r, r0.Offset(0, -1)).Copy
Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
Do
Set r = r0.Offset(0, 1)
Set r0 = .Rows("1:1").Cells.FindNext(r0)
If r0 Is Nothing Then Exit Do
If r0.Address = add Then Exit Do
Range(r, r0.Offset(0, -1)).Copy
Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
Set r = r0.Offset(0, 1)
Loop
Range(r, r.End(xlToRight)).Copy
Worksheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
 Worksheets("sheet2").Range("A1").EntireRow.Delete
 testone
End Sub

Code:
Sub testone()
Dim j As Long
With Worksheets("sheet2")
For j = 1 To Range("A1").End(xlDown).Row
If WorksheetFunction.CountA(Range(.Cells(j, 1), .Cells(j, 1).End(xlToRight))) <= 3 Then
.Cells(j, 1).End(xlToRight).Offset(0, 1) = 0
End If
Next j
End With
End Sub


Code:
Sub undo()
Worksheets("sheet2").Cells.Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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