Home » excel » excel – What causes an entire ListBox column to be blank?

excel – What causes an entire ListBox column to be blank?

Posted by: admin May 14, 2020 Leave a comment


I’m working on an Excel project where I have a VBA UserForm with two ListBox controls. I load them by assigning an array to the List property. All of the expected data shows up, except for the seventh column which is entirely blank.

Why is this entire column blank?

Additional details:
The array has around 15 columns and 25 rows.
The seventh column of the array is listed in Locals as type Variant/Decimal. It contains UPC values (up to 14 digits) stored in the database as DECIMAL(14,0).
None of the UPC values are NULL or blank. They DO show up in the array in the Locals window and they can be retrieved from the ListBox, just not seen.
The ListBox has ListStyle fmListStylePlain and MultiSelect fmMultiSelectMulti.
Although you cannot Dim (declare) a variable as Decimal, you can convert to Decimal with the CDec function: ?typename(CDec(4.5)) in the Immediate window.

Minimal Example:
Create a UserForm with a ListBox (Listbox1), two TextBox controls (TextBox1 and TextBox2), and two Command Buttons (CommandButton1 and CommandButton2).
In the Code Module of the UserForm, paste in the below code:

Private Sub CommandButton1_Click()
    Dim x(0 To 0, 0 To 0)

    x(0, 0) = CDec(TextBox1.Text)
    ListBox1.List = x

    TextBox2.Text = ListBox1.List(0, 0)
End Sub

Private Sub CommandButton2_Click()
    Dim x(0 To 0, 0 To 0)

    x(0, 0) = CDbl(TextBox1.Text)
    ListBox1.List = x

    TextBox2.Text = ListBox1.List(0, 0)
End Sub

Annotated Screenshot

Run the UserForm, type a number (with or without decimal places) in TextBox1, then click CommandButton1. ListBox1 remains blank, but Textbox2 shows a value.
Click CommandButton2. ListBox1 and TextBox2 show the value.

How to&Answers:

Apparently, a VBA ListBox cannot handle Variant/Decimal values. I haven’t been able to find an authoritative source to explain this, but I’ve found that converting the Decimal values to text using the Format function was effective.

Dim vData as Variant
Dim lRow as Long

'fill vData array from database here

'convert seventh column to zero-padded 14-character string in this loop:
For lRow = LBound(vData,1) to UBound(vData,1)
    vData(lRow, 6) = Format(vData(lRow, 6), "00000000000000")
Next lRow