Getting "object variable or with variable not set for combobox

Caveman1964

Board Regular
Joined
Dec 14, 2017
Messages
127
Office Version
  1. 2016
Platform
  1. Windows
I have tried to resolve this through threads and videos but can't get it to work. My understanding of set and objects are not there yet.
I am getting the error "object variable or with variable not set when I click the combobox selection to put in cell.
Here is my code and would appreciate any help and guidence.
My task I am wanting to do is,
Click any one of 3 cells, a form pop up with a combobox with selections, just click the selection and it goes in the active cell and form disappears.


Private Sub ComboBox1_Change()
Dim selectedOption As String
selectedOption = ComboBox1.Value

' Get the active cell
Dim activeCell As Range
Set activeCell = activeCell

' Place the selected option into the active cell
activeCell.Value = selectedOption
End Sub

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "Apple"
.AddItem "Orange"
.AddItem "Pear"
End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A4")) Is Nothing Then
' Show the user form
UserForm1.Show
End If
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I am not sure if it is the only problem with your code, but NEVER, EVER do this!
VBA Code:
Dim activeCell As Range
Set activeCell = activeCell
You should NEVER use reserved words (words already used by Excel VBA for other things, like methods, properties, and objects) for the name of your variables, procedures, and functions.

"ActiveCell" is a pre-defined default range object in VBA, referring to the active cell on your sheet.
The issue is that if you create a variable named "activecell" (case does not matter) like you did, then in your code whenever you reference "activecell", Excel cannot tell if you mean the default "ActiveCell", or your variable. This ambiguity can cause errors and/or unexpected results.

Choose a different name for your variable.

Or if you are just trying to assign it to whatever actually is the activecell, get rid of the whole variable declaration and assignment, so you are just left with this:
VBA Code:
Private Sub ComboBox1_Change()
Dim selectedOption As String
selectedOption = ComboBox1.Value

' Place the selected option into the active cell
ActiveCell.Value = selectedOption

End Sub
(there is no need to "get" the active cell, by default "ActiveCell" is the active cell!)
 
Upvote 0
VBA Code:
Private Sub ComboBox1_Change()
Dim selectedOption As String
selectedOption = ComboBox1.Value

' Place the selected option into the active cell
activeCell.Value = selectedOption
End Sub
activeCell is already an predefined object/class. So you do not need to try to set it to itself.
 
Upvote 0
I am not sure if it is the only problem with your code, but NEVER, EVER do this!
VBA Code:
Dim activeCell As Range
Set activeCell = activeCell
You should NEVER use reserved words (words already used by Excel VBA for other things, like methods, properties, and objects) for the name of your variables, procedures, and functions.

"ActiveCell" is a pre-defined default range object in VBA, referring to the active cell on your sheet.
The issue is that if you create a variable named "activecell" (case does not matter) like you did, then in your code whenever you reference "activecell", Excel cannot tell if you mean the default "ActiveCell", or your variable. This ambiguity can cause errors and/or unexpected results.

Choose a different name for your variable.

Or if you are just trying to assign it to whatever actually is the activecell, get rid of the whole variable declaration and assignment, so you are just left with this:
VBA Code:
Private Sub ComboBox1_Change()
Dim selectedOption As String
selectedOption = ComboBox1.Value

' Place the selected option into the active cell
ActiveCell.Value = selectedOption

End Sub
(there is no need to "get" the active cell, by default "ActiveCell" is the active cell!)
Ah ok! thank you for advice!
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0
VBA Code:
Private Sub ComboBox1_Change()
Dim selectedOption As String
selectedOption = ComboBox1.Value

' Place the selected option into the active cell
activeCell.Value = selectedOption
End Sub
activeCell is already an predefined object/class. So you do not need to try to set it to itself.
Thank You!
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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