In VBA how do I set a cell's value to be whatever is selected in a combobox form?

In the code below, how I use line 12 to determine the value of a cell in my sheet? I'm getting an error because I'm not calling the combobox correctly.

    Dim productInputValue
    Dim marketInputValue
    Dim guildInputValue
    Dim revenueInputValue
    Dim yearInputValue
    Dim quarterInputValue
    Dim episodeCountInputValue
    
    Range("A100000").End(xlUp).Offset(1, 0).Select
    
    productInputValue = InputBox("Product?")
    marketInputValue = ComboBox1.Value
    revenueInputValue = InputBox("Revenue?")
    'yearInputValue = InputBox("Year?")
    'quarterInputValue = InputBox("Quarter?")
    episodeCountInputValue = InputBox("Episode Count?")
    guildInputValue = InputBox("Guild?")

    ActiveCell.Value = productInputValue
    ActiveCell.Offset(0, 1).Value = marketInputValue
    ActiveCell.Offset(0, 2).Value = "US00"
    ActiveCell.Offset(0, 3).Value = "R"
    ActiveCell.Offset(0, 4).Value = revenueInputValue
    ActiveCell.Offset(0, 5).Value = 0
    ActiveCell.Offset(0, 6).Value = 2014
    ActiveCell.Offset(0, 7).Value = 4
    ActiveCell.Offset(0, 8).Value = guildInputValue

Open in new window

shampouyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David L. HansenCEOCommented:
Dim marketInputValue As String
0
NorieAnalyst Assistant Commented:
Where is this code located and where is the combobox located?
0
shampouyaAuthor Commented:
This code is in Module1 and the combobox is in UserForm1, both part of the same sheet.
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

David L. HansenCEOCommented:
You might want to use an IF statement surrounding line 12 that checks for null values.

Like this:
IF IsNull(Combobox1.Value) = False Then 
marketInputValue = Combobox1.Value 
End If

Open in new window

And make sure you specify change your dim statement to "Dim marketInputValue As String."
0
shampouyaAuthor Commented:
How do I make the combobox pop up so that the user is prompted to do something?
0
David L. HansenCEOCommented:
Is it not showing? Usually the user must click on the combobox control before it will dropdown to display the choices.
0
shampouyaAuthor Commented:
This combobox is not linked to any cell within the sheet, it is simply intended to pop up when the macro runs. I created it by right clicking the vba project window and selecting Insert > UserForm
0
David L. HansenCEOCommented:
If there isn't any data/choices in the combobox (note: you can hardcode the choices or link it to a datasource) then the combobox can not display anything. Do you want it just to open up empty? That might be possible, but I think you'd at least need to fill it with some blanks.
0
shampouyaAuthor Commented:
Sure that's fine, how would I have the combobox show up on the screen and show blank values, or show "a", "b", and "c" as the choices when the macro is run?
0
David L. HansenCEOCommented:
With ComboBox1
.AddItem "a"
.AddItem "b"
.AddItem "c"
End With
End Sub

Open in new window

0
NorieAnalyst Assistant Commented:
You need to show the userform which can be done with code like this.
UserForm1.Show

Open in new window


Also, the code you posted should be in the userform module, not a standard module.

You could call it from a command button on the userform, for example.
Private Sub CommandButton1_Click()
    Dim productInputValue
    Dim marketInputValue
    Dim guildInputValue
    Dim revenueInputValue
    Dim yearInputValue
    Dim quarterInputValue
    Dim episodeCountInputValue
    
    Range("A100000").End(xlUp).Offset(1, 0).Select
    
    productInputValue = InputBox("Product?")
    marketInputValue = ComboBox1.Value
    revenueInputValue = InputBox("Revenue?")
    'yearInputValue = InputBox("Year?")
    'quarterInputValue = InputBox("Quarter?")
    episodeCountInputValue = InputBox("Episode Count?")
    guildInputValue = InputBox("Guild?")

    ActiveCell.Value = productInputValue
    ActiveCell.Offset(0, 1).Value = marketInputValue
    ActiveCell.Offset(0, 2).Value = "US00"
    ActiveCell.Offset(0, 3).Value = "R"
    ActiveCell.Offset(0, 4).Value = revenueInputValue
    ActiveCell.Offset(0, 5).Value = 0
    ActiveCell.Offset(0, 6).Value = 2014
    ActiveCell.Offset(0, 7).Value = 4
    ActiveCell.Offset(0, 8).Value = guildInputValue
End Sub

Open in new window


To populate the combobox use the form's Initialize event.
Private Sub UserForm_Initialize()
    ComboBox1.List = Array("A", "B", "C")
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.