Auto-run a VBA function when a cell value is changed

mcruse

New Member
Joined
Jan 31, 2008
Messages
6
Hi,

I have a function made up of if-thens that adds different text to a cell when the value of another cell is changed through a drop down menu. But it only runs when I hit the play button in VBA. I've trying searching for the worksheet change event but I can't figure out how to incorporate it into my code. Any help would be greatly appreciated!

The basic code is
Function Optimum()
Range("C2").Select
If Range("B2") = "2.5" Then
Range("C2").FormulaR1C1 = "Use 1 2kg and 1 .5kg"
Else
If Range("B2") = 5 Then
Range("C2").FormulaR1C1 = "Use 1 5kg"
Else
.
.
.
End If
.
.
.
End Function
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I'm assuming you've already got your listbox/combobox linked to the B2? Put this into your worksheet module. Change combobox1 to the name of your control.

Code:
Private Sub [COLOR=darkgreen][B]ComboBox1[/B][/COLOR]_Change()
 
If [COLOR=darkgreen][B]ComboBox1[/B][/COLOR] = "2.5" Then
    Range("C2").Select
    Range("C2") = "Use 1 2kg and 1 .5kg"
 
ElseIf [COLOR=darkgreen][B]ComboBox1[/B][/COLOR] = 5 Then
    Range("C2").Select
    Range("C2") = "Use 1 5kg"
 
End If
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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