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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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