List Box help

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
I have a listbox on a form which I load values using a loop in VBA. The values are in Sheet1!A1:A25. The property list box is set to ColumnHead = True. This works fine showing the Column headers at the beginning of the list. However, I have another listbox on a worksheet in which the ColumnHead is also set to True. But I don't get the Column Headers. What could be wrong? Of course, I'm using values from another Sheet2!A1:A30. What am I missing?
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Are you sure what you are seeing in the first row are actually headers?

Normally the only way to display headers is to set the RowSource property to the range.

If you populate the listbox using other methods then the headers might appear but they'll be regarded as just another item in the list.

For both of the controls can you post the code you are using to populate them?
 
Upvote 0
This is a partial code that I use to load the listbox. This listbox is on my worksheet. Please note that towards the bottom of the code, I had to comment out the .Rowsource because it errored. I was able to reference the .Rowsource property when the listbox was used on the form, however.

Code:
 Dim Data(1 To 200, 1 To 5) As Variant
  
  For i = 1 To Num
     Data(i, 1) = Worksheets(ws).Range("A" & i)
  Next i
  
  
  'Load last name
  For i = 1 To Num
     Data(i, 2) = Worksheets(ws).Range("B" & i)
  Next i
  
  'Load first name
  For i = 1 To Num
     Data(i, 3) = Worksheets(ws).Range("C" & i)
  Next i
  
  'Load Address
  For i = 1 To Num
     Data(i, 4) = Worksheets(ws).Range("D" & i)
  Next i
  
  'Load Notes
  For i = 1 To Num
     Data(i, 5) = Worksheets(ws).Range("E" & i)
  Next i
  
   
  Sheets("Sheet3").ListBox1.ColumnCount = 5
  Sheets("Sheet3").ListBox1.List = Data
  
   
  ColCnt = 5
  Set rng = ActiveSheet.Range("A2:" & "E" & i)
  With Sheets("Sheet3").ListBox1
        .ColumnCount = ColCnt
     '   .RowSource = rng.Address
        cw = ""
        For c = 1 To .ColumnCount
            cw = cw & rng.Columns(c).Width & ";"
        Next c
        .ColumnWidths = cw
        .ColumnHeads = True
        .ListIndex = 0
    End With
 
Upvote 0
Dave

You have to use RowSource to get headers wherever the control is located.

The difference here seems to be that you are having trouble referring to the RowSource for the listbox on the worksheet.

What type of control is it? Forms? ActiveX?
 
Upvote 0
I'm not completely sure if it's forms control or ActiveX. The listbox that I'm using on the Worksheet has a property component and the other does not. So is this forms or ActiveX?? When I add the listbox on the Worksheet, I don't see the Rowsource property but I can see the other property values, such as setting the Columnhead. On the other hand, when I add the listbox on the userform, I do see the Rowsource property. So, if I need to access the Rowsource property on the listbox, why is it not there when I use on the Worksheet?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top