Combo Box Fill in Header

JarekM

Board Regular
Joined
Nov 13, 2018
Messages
86
Hi, I have a problem with my combo box. In the screenshot that I have provided, you can see "Contact Person", "Basic Wage" and "Time Wage". I want these items to fill the header in the combo box. Can someone help me? Thank you.

Here is the code that I use:
VBA Code:
Public Sub cmFillWorker1(cmName As Object)
    Set WbDb = Workbooks("DataBase.xlsx")
    Set ShDb = WbDb.Sheets("TbVendors")
'    Set WbCd = ThisWorkbook
'    Set shCd = WbCd.Sheets("shSummary")

'----------------------------------------------
    'load CombBox4 VENDOR NAME mine Setup  Unit
    Dim rowStart
    With cmName
        rowStart = 6
        .Clear
        Do Until ShDb.Cells(rowStart, "d") = ""
        .ColumnHeads = True
            .columnCount = 4
            .ColumnWidths = "106;84;48;0"
            .AddItem
            .list(.ListCount - 1, 0) = ShDb.Cells(rowStart, "d")
            .list(.ListCount - 1, 1) = ShDb.Cells(rowStart, "ac")
            .list(.ListCount - 1, 2) = ShDb.Cells(rowStart, "ad")
'            .list(.ListCount - 1, 3) = sheet_Setup.Cells(rowStart, "bf")
'            .list(.ListCount - 1, 1) = sheet_Setup.Cells(rowStart, "bd")
            rowStart = rowStart + 1
        Loop
    End With
'----------------------------------------------

    '    WbDb.Close
    Set WbDb = Nothing
    Set ShDb = Nothing
    Set WbCd = Nothing
End Sub

uf.png
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi @JarekM.
Thanks for posting on the board.​

I want these items to fill the header in the combo box
The ListBox only displays column headers if you use RowSource property. Otherwise, they are not available.

--------------------------------​

I show you 3 ways to display headers.
The 3 options is assuming that the headers are in row 6.

--------------------------------​
1. The first option. You are already using it, just remove this instruction:
VBA Code:
.ColumnHeads = True


The problem with this option, as you've already seen, is that the headers are in the listbox as an item.​
Example:​
1685796378915.png




--------------------------------
2. The second option. Use the headers with the ColumnHeads and RowSource properties.
This way you will be able to see the headers as headers and not as an item:​
1685796854080.png
To achieve the above and noting that the information that you are going to load in the combo is not continuous columns, then you must create an auxiliary sheet.​
In the code I put comments for your better understanding:​
VBA Code:
Public Sub cmFillWorker1(cmName As MSForms.ComboBox)
  Dim WbDb As Workbook
  Dim shdb As Worksheet, shax As Worksheet
  
  Set WbDb = Workbooks("DataBase.xlsx")
  Set shdb = WbDb.Sheets("TbVendors")
  
  Set shax = WbDb.Sheets("Aux")                           'Auxiliar sheet, The sheet can be hidden
  
  shax.Cells.ClearContents
  shdb.Range("D:D, AC:AC, AD:AD").Copy shax.Range("A1")   'Here you put the columns you need to see in the combobox.
  
  With cmName
    .ColumnHeads = True                                   'enable columnhead
    .ColumnCount = 4
    .ColumnWidths = "106;84;48;0"
    
    'Assuming the headers are in row 6, then the data starts at row 7.
    'Rowsource automatically loads the headers from the previous row.
    .RowSource = shax.Name & "!A7:D" & shax.Range("A" & Rows.Count).End(3).Row
  End With
  
  Set WbDb = Nothing
  Set shdb = Nothing
  Set shax = Nothing
End Sub


--------------------------------
3. The third option. It is using labels.
If you don't want to create the auxiliar sheet, then another option is to add the headings in labels:​
1685797643928.png
For above, use this code:
VBA Code:
Public Sub cmFillWorker1(cmName As msforms.ComboBox)
  Dim WbDb As Workbook
  Dim shdb As Worksheet
  Dim rowStart As Long

  Set WbDb = Workbooks("DataBase.xlsx")
  Set shdb = WbDb.Sheets("TbVendors")
  With cmName
  rowStart = 7
  .Clear
  Do Until shdb.Cells(rowStart, "D") = ""
    .ColumnCount = 4
    .ColumnWidths = "106;84;48;0"
    .AddItem
    .List(.ListCount - 1, 0) = shdb.Cells(rowStart, "D")
    .List(.ListCount - 1, 1) = shdb.Cells(rowStart, "AC")
    .List(.ListCount - 1, 2) = shdb.Cells(rowStart, "AD")
    rowStart = rowStart + 1
  Loop
  End With
  Set WbDb = Nothing
  Set shdb = Nothing
End Sub

--------------
Review which is the best option for you.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
Hi @JarekM.
Thanks for posting on the board.​


The ListBox only displays column headers if you use RowSource property. Otherwise, they are not available.

--------------------------------​

I show you 3 ways to display headers.
The 3 options is assuming that the headers are in row 6.

--------------------------------​
1. The first option. You are already using it, just remove this instruction:
VBA Code:
.ColumnHeads = True


The problem with this option, as you've already seen, is that the headers are in the listbox as an item.​
Example:​




--------------------------------
2. The second option. Use the headers with the ColumnHeads and RowSource properties.
This way you will be able to see the headers as headers and not as an item:​
To achieve the above and noting that the information that you are going to load in the combo is not continuous columns, then you must create an auxiliary sheet.​
In the code I put comments for your better understanding:​
VBA Code:
Public Sub cmFillWorker1(cmName As MSForms.ComboBox)
  Dim WbDb As Workbook
  Dim shdb As Worksheet, shax As Worksheet
 
  Set WbDb = Workbooks("DataBase.xlsx")
  Set shdb = WbDb.Sheets("TbVendors")
 
  Set shax = WbDb.Sheets("Aux")                           'Auxiliar sheet, The sheet can be hidden
 
  shax.Cells.ClearContents
  shdb.Range("D:D, AC:AC, AD:AD").Copy shax.Range("A1")   'Here you put the columns you need to see in the combobox.
 
  With cmName
    .ColumnHeads = True                                   'enable columnhead
    .ColumnCount = 4
    .ColumnWidths = "106;84;48;0"
   
    'Assuming the headers are in row 6, then the data starts at row 7.
    'Rowsource automatically loads the headers from the previous row.
    .RowSource = shax.Name & "!A7:D" & shax.Range("A" & Rows.Count).End(3).Row
  End With
 
  Set WbDb = Nothing
  Set shdb = Nothing
  Set shax = Nothing
End Sub


--------------------------------
3. The third option. It is using labels.
If you don't want to create the auxiliar sheet, then another option is to add the headings in labels:​
For above, use this code:
VBA Code:
Public Sub cmFillWorker1(cmName As msforms.ComboBox)
  Dim WbDb As Workbook
  Dim shdb As Worksheet
  Dim rowStart As Long

  Set WbDb = Workbooks("DataBase.xlsx")
  Set shdb = WbDb.Sheets("TbVendors")
  With cmName
  rowStart = 7
  .Clear
  Do Until shdb.Cells(rowStart, "D") = ""
    .ColumnCount = 4
    .ColumnWidths = "106;84;48;0"
    .AddItem
    .List(.ListCount - 1, 0) = shdb.Cells(rowStart, "D")
    .List(.ListCount - 1, 1) = shdb.Cells(rowStart, "AC")
    .List(.ListCount - 1, 2) = shdb.Cells(rowStart, "AD")
    rowStart = rowStart + 1
  Loop
  End With
  Set WbDb = Nothing
  Set shdb = Nothing
End Sub

--------------
Review which is the best option for you.
Cordially
Dante Amor
--------------​
Hi Dante, thank you for the quick reply and the several options that you have given me. I found that the second option worked the best for me.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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