Hi,
I am trying to load a multicolumn combobox with the following code
' load all open work orders in a multi-column combobox
Dim MyArray As Variant, tRow As Long, cRow As Long, d As Integer
tRow = Worksheets("WO").Range("A" & Rows.Count).End(xlUp).Row
If tRow > 2 Then
ReDim MyArray(1 To 4, 1 To tRow - 2) ' declare MyArray as a dynamic array
' define the properties of the combobox
With Me.RcptCmbWoNo
.ColumnCount = 4
.ListWidth = 320
.ListRows = 6
.ColumnWidths = "20;60;200;40"
End With
' populate the combobox
d = 1
For cRow = 3 To tRow + 1
If Worksheets("WO").Cells(cRow, 23).Value > 0 Then
MyArray(1, d) = Worksheets("WO").Cells(cRow, 1).Value
MyArray(2, d) = Worksheets("WO").Cells(cRow, 3).Value
MyArray(3, d) = Worksheets("WO").Cells(cRow, 4).Value
MyArray(4, d) = Worksheets("WO").Cells(cRow, 23).Value
d = d + 1
End If
Next cRow
ReDim Preserve MyArray(1 To 4, 1 To d - 1)
' populate the combobox
If d > 0 Then
RcptCmbWoNo.List() = Application.Transpose(MyArray)
Else
MsgBox "There are no work orders to display !"
MainMenu
Unload RcptForm
End If
Else
MsgBox "There are no work orders to display !"
MainMenu
Unload RcptForm
End If
End Sub
As you would see I am loading columns 1,3,4 and 23 from worksheet "WO" if the value in column 23 is greater than 0. The first two rows of this worksheet contain the header information. The data starts from row 3 onwards. This code works most of the time except that I am facing two situations as under:
a) If there is no data in the worksheet (last used row is 2), I get "Run-time error '91': Object variable or With block variable not set." I get the same error if there is data but none fit the criterion "column 23 > 0".
b) if there are 2 or more records to be displayed in the combobox, the information is displayed correctly in the columns. However, if there is only one record, each column is displayed one below the other as a row and I am able to select any row.
I have been trying to get over this problem but do not seem to find the right solution.
Would be grateful if some one can point out why I getting these problems and edit the code suitably for me.
Thanks in advance,
Anand Hattangady
Bookmarks