• Status: Solved
  • Priority: Medium
  • Security: Private
  • Views: 9
  • Last Modified:

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

0
shampouya
Asked:
shampouya
  • 5
  • 4
  • 2
1 Solution
 
David L. HansenProgrammer AnalystCommented:
Dim marketInputValue As String
0
 
NorieVBA ExpertCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
David L. HansenProgrammer AnalystCommented:
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. HansenProgrammer AnalystCommented:
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. HansenProgrammer AnalystCommented:
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. HansenProgrammer AnalystCommented:
With ComboBox1
.AddItem "a"
.AddItem "b"
.AddItem "c"
End With
End Sub

Open in new window

0
 
NorieVBA ExpertCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now