--- In vbhelp@yahoogroups.com, "jim8440" <james8570@...> wrote:
>
> Suppose I have some numbers from 1 to 10 in an array (but it could be
> of any size). I want them to be formatted into 3 columns (but it could
> be of any # of columns), but in a particular way.
>
> Array is:
> xyz[1] =1
> xyz[2] =2
> xyz[3] =3
>
> and so on....
>
> Example:
>
> Instead of this format (I've figured out how to format this
> programatically) :
> 1 5 9
> 2 6 10
> 3 7
> 4 8
>
> I would prefer it this way:
> 1 5 8
> 2 6 9
> 3 7 10
> 4
>
>
> Thank you in advance for your help!
>
This code sample assumes you have a textbox
and a flexgrid on your form that "each" takes
up roughly half of the height of the form and
the width of each is roughly the width of the
form.
'Be aware of possible line-wrapping.
'Be aware of possible unwanted space(s).
'Adjust to suit your needs.
'Start of Code
Private Sub Form_Load()
Dim i As Long, n As Long, r As Long, c As Long
For i = 0 To MSFlexGrid1.Cols - 1
MSFlexGrid1.ColWidth(i) = 922
Next
For i = 0 To MSFlexGrid1.Rows - 1
MSFlexGrid1.RowHeight(i) = 300
Next
Dim ma As Long
ma = 16 'ubound of numeric array (randomly chosen for this
demo. adjust flexgrid accordingly if changed.)
Dim mf As Long
mf = 4 'maximum number of items in the first column
Dim mr As Long
mr = 3 'maximum number of items in the remaining columns
ReDim NumArray(ma) As Long
'fill the numeric array for this demo
For i = 0 To UBound(NumArray) - 1
NumArray(i) = i + 1
Next
'for textbox (or richeditbox) it's easier to create strings
'that can be used to display the numeric array as formatted
ReDim StrArray(mf) As String
For i = 0 To UBound(NumArray) - 1
If i < mf Then
StrArray(i) = Format(NumArray(i), "00")
Else
n = (i - mf) Mod mr
StrArray(n) = StrArray(n) + Chr(9) + Format(NumArray(i), "00")
End If
Next
Text1.Text = ""
For i = 0 To UBound(StrArray) - 1
Text1.Text = Text1.Text & StrArray(i) & vbCrLf
Next
'for flexgrid (or listview) it's easier to just
'figure out where the data should be displayed
c = 0
For i = 0 To UBound(NumArray) - 1
If i < mf Then
MSFlexGrid1.TextMatrix(i, c) = Format(NumArray(i), "00")
Else
r = ((i - mf) Mod mr)
If r = 0 Then c = c + 1
MSFlexGrid1.TextMatrix(r, c) = Format(NumArray(i), "00")
End If
Next
End Sub
'End Of Code
David