Trying to populate a textbox dynamically not working

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Code:
Private Sub cmbSDPFLine_Enter()

    Dim rng As Range
    Dim rcell As Range
    
    Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:F1")
    cmbSDPFLine.Clear
    
    For Each rcell In rng.Cells
        cmbSDPFLine.AddItem = rng.Cells.Value
        If rcell.Value = "Line 4" Then
            With cmbSDPFLine
                'Nothing here yet
            End With
        End If
    Next rcell


End Sub
A brief description of what this code is suppose to do. Simply put, I just need it to populate the combobox with the values in the range A1-F1. But I am getting a "Compile Error: Expected Function or variable" on the following line with the ".AddItem=" highlighted in blue:
Code:
cmbSDPFLine.AddItem = rng.Cells.Value

Thank You
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try:
Code:
cmbSDPFLine.AddItem = rcell.Value
 
Upvote 0
Thank you for the response but I am still getting a compile error on the following code.
Code:
[COLOR=#333333].AddItem =[/COLOR]
 
Upvote 0
Code:
Private Sub UserForm_Initialize()    
      cmbSDPFLine.List = Sheets("Sheet1").Range("A1:F1").Value
End Sub

I even tried the code above just to see what it would do.
The Range("A1:F1") contain values but only adds the value of cell A1 to the combobox.
 
Last edited:
Upvote 0
Try:
Code:
Private Sub UserForm_Initialize()
    With Me.cmbSDPFLine
        .Clear
        .List = WorksheetFunction.Transpose(Sheets("Sheet1").Range("A1:F1").Value)
    End With
End Sub
Change the sheet name to suit your needs.
 
Upvote 0
That worked great. Thank you much. A couple of questions, so I can learn what that code does. That being said, what does the below code do exactly.
Code:
WorksheetFunction.Transpose
 
Upvote 0
You are very welcome. :) The UserForm_Initialize means that whenever the user form is activated, the combobox will populate with the values in the range. Since the range you mentioned (A1:F1) is horizontal, the WorksheetFunction.Transpose function converts it into vertical form to insert into the combobox. I hope this makes sense.
 
Upvote 0
I'm glad that another solution was found, but for general information, the .AddItem method does not take a equals sign.
The OP code could be modified to

Code:
cmbSDPFLine.AddItem rng.Cells.Value
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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