[RESOLVED] VBA (Excel) - Filling a Listbox with info from another workbook

Hi all,

I am new to VBA/Excel, and am just starting to work on some macro's. I have made a macro to create a listbox, filled with information from another worksheet/workbook. Not that hard. The problem is this:

I need to get every entry in column D, until I see a cell with Backgroundcolor 3 (red). Before that red cell and in between the cells filled with words, lots of cells are empty (on a range of D4:D390 for example, only 15 cells contain info). The range should be dynamic.

I tried a do-while until Cells(i,4).Interior.ColorIndex =3, but that did not work, so this was my 2nd guess:

Private Sub UserForm_Initialize()

Dim ListItems As Variant, i As Long
Dim Source As Workbook, r As Integer

With Me.listChangeMachine
.Clear
Application.ScreenUpdating = False

Set Source = Workbooks.Open("C:\Documents\test.xls", False, True)

ListItems = Source.Worksheets("MP").Range("D5:D390").Value

i = 1

For i = 1 To UBound(ListItems)
If ListItems(i) = "" Then
'Delete ListItems(i)
i = i + 1
Else
i = i + 1
End If
Next

Source.Close False
Set Source = Nothing

Application.ScreenUpdating = True

ufChangeMachine.listChangeMachine.List = Application.WorksheetFunction.Transpose(ListItems)

End With

End Sub

I do not know what I'm doing wrong. Every time I change something, another error will pop up. Can anyone help me with that? Thanks so much!

J.
[1693 byte] By [jvd00] at [2007-11-20 10:13:15]
# 1 Re: [RESOLVED] VBA (Excel) - Filling a Listbox with info from another workbook
You have to remove items in REVERSE:
Also, there are not Ubound() items!

For i = UBound(ListItems)-1 to 1 Step -1
dglienna at 2007-11-9 19:35:25 >
# 2 Re: [RESOLVED] VBA (Excel) - Filling a Listbox with info from another workbook
Thnx a lot for your post. However, I changed the whole thing now, because it had to be a dynamic array. Now it looks like this:

[code]Cells(5, 4).Activate

i = 0

Do While ActiveCell.Interior.ColorIndex <> 3

If ActiveCell.Value = "" Then
ActiveCell.Offset(1, 0).Activate
Else
ReDim Preserve ListItems(i)
ListItems(i) = ActiveCell.Value + " - " + ActiveCell.Offset(0, 2).Value
ActiveCell.Offset(1, 0).Activate
i = i + 1

End If
Loop[\code]

And it works!

Thnx, J.
jvd00 at 2007-11-9 19:36:30 >