简体   繁体   中英

How to Populate VBA Combo box dropdown in user form from Excel data

I am new to VBA coding. Can some one tell me ,how to populate the combo box in the user form with the values in the excel sheet.

Say sheet name as "Reg ALL - current" and I need to populate the value from cell AI(which is a date column). Also I had to increment the date to 1day from AJ to cell BF.

Example : if AI holds value (19/06/2019) then AJ should hold (20/06/2019) and so on until BF.

Can anyone tell me ,how to handle this code in VBA?

There are different ways. If you have one set range of cells (that's what I assume reading your question) that won't change, you could just set the RowSource property of your combobox.

For example:

在此处输入图片说明

在此处输入图片说明

Apply to your situation:

  • Cell AI1 holds your date
  • Cell AJ1 holds formula =AI1+1
  • Drag formula to cell BF1 (assuming you always want to add to the value in AI1 , the formula will keep doing this for you)
  • Use RowSource property and fill in =Sheet1!AI1:BF1

Conclusion, no VBA needed at all! If I understood your question well enough that is.

Here is simple solution Just add a button, paste this.

Dim i As Long

'Clear existing items
ComboBox1.Clear

'36 (AJ) column to 58 (BF) column
For i = 36 To 58

    ComboBox1.AddItem ActiveSheet.Cells(1, i).Value

Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM