View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Dealing with unknown array sizes

Hi Robert,

The only way I can think of is to firstly find the first blank cell in the
array and then dimension another array to the correct size and copy the data
into it.

Sub RemoveBlanksFromArray()
Dim myArr(1 To 500, 1 To 5)
Dim myListArr()
Dim r As Long
Dim c As Long

'I have used dummy data to create an array
For r = 1 To 500
For c = 1 To 5
myArr(r, c) = Cells(r, c)
Next c
Next r

'Test for first blank element
For r = 1 To 500
If myArr(r, 1) = "" Then
Exit For
End If
Next r

'Subtract 1 from r because r is where
'the blank element was found.
r = r - 1

'Redimension a new array
ReDim myListArr(1 To r, 1 To 5)

'Copy the data into a new array
For r = 1 To UBound(myListArr, 1)
For c = 1 To 5
myListArr(r, c) = myArr(r, c)
Next c
Next r

'Use the new array for the listbox
With UserForm1
.ListBox1.List = myListArr()
.Show
End With

'Or on a worksheet
'Use the new array for the listbox
'With Sheets("Sheet2")
' .ListBox1.Object.List = myListArr()
'End With


End Sub

--
Regards,

OssieMac