Change event problem

julhs

Active Member
Joined
Dec 3, 2018
Messages
407
Office Version
  1. 2010
Platform
  1. Windows
I need some help with a certain aspect of using the codes below
My conundrum is; using a Data Validation list to select either "m2" or "ha" and using a straight "Change" event the "ComboBox15.Visible = False" line fires immediately.
But when I use an ActiveX Combobox to make the selection from I need to use a "SelectionChange" event
BUT
it requires 2 additional click events for "ComboBox15.Visible = False" to be initiated.

Where or what is it I'm missing with the Combox method that requires the additional click events?

Code for Data Validation list change:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("g18")
If rng = "ha" Then
ComboBox15.Visible = False
End If
End Sub
Code for ActiveX Combobox change:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("g18")
If rng = "ha" Then
ComboBox15.Visible = False
End If
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The SelectionChange event is for selecting a cell. Selecting or changing a combobox will not fire that event. If you want code to run when an item in the combobox is changed, use the ComboBox15_Change event instead.
 
Upvote 0
Solution
Hi 6stringjazzer
Thanks for response

But that’s the conundrum!!
The “Change” event code above works if I use a Data Validation list,
Eg Make a selection change in DV list to “ha” >> ComboBox15 is “Hidden”
BUT!!!! if I use a ComboBox instead of a Data Validation List to make the selection, then ComboBox15 is NOT hidden??
Only thing I can think of is the syntax required for the ComboBox way is different to that of a DV list?
 
Upvote 0
First of all you may misunderstand what "Selection" means here.

The SelectionChange does not occur because you "select" a different item in a cell having data validation with a dropdown list. It occurs when you click on a cell that is different than the last active cell.

  1. Current active cell is A1. You click on B2. Worksheet_SelectionChange occurs.
  2. Current active cell is G18. You pick an item from the dropdown list. Worksheet_SelectionChange does not occur, but Worksheet_Change occurs.
  3. Current active cell is A1. You click on the combobox and pick an item. ComboBox15_Change occurs. Worksheet_SelectionChange and Worksheet_Change do not occur.

Only thing I can think of is the syntax required for the ComboBox way is different to that of a DV list?
Totally. To detect a change in what is picked in a ComboBox use Sub ComboBox15_Change()

If you explain what you actually want to do, rather than showing code that does not do what you want to do, we can provide the appropriate code.
 
Upvote 0
Hi Jeff
Your right when it comes to “What constitutes a Selection”!!
From your last post I just picked up on your ref to ComboBox15_Change, (missed its significance in post # 2)
Will experiment with changes that are necessary.
If I have no joy; then I will upload an example, but both will take me a while (particularly a Xl2bb as I will need to bare down the actual Worksheet to its barebones)
I’ll get back to you tomorrow
Thanks
Julhs
 
Upvote 0
I think XL2BB doesn't support comboboxes or other controls. If you just write a walkthrough of the scenario you want to happen (what actions the user takes, what occurs as a result) I think I can help with the code. I think you're pretty close as it is.
 
Upvote 0
While I’ve not tested ComboBox15_Change, I‘ve done a xl2dd that at least gives a sort of picture?
The crux to all this is: if G18 = “ha” then Combobox 16 is hidden, or if G18 = ”m2” then Combobox 15 is hidden
This is the peared down sheet.
It may help to a degree!!!!!!!
The Ornage cells are the CB drop down locations and the Yellow cells are thier rusults
00 Fert-Chemical-Nozzle calculator NEW Currant.xlsm
EFGHIJKLMNOPQRSTU
7
8 This is reference section for ComboBox 15 & 16
9
10ComboBox14This is reference section for ComboBox14 list
11
12Select a Format belowml m2lts m2 of a ha
13m20.0100500.1000.0050
14ha0.0103490.1030.0049
150.0105480.1050.0048
160.0107470.1070.0047
170.0109460.1090.0046
18= CB 14 selection resultm20.0112450.1120.0045
190.0114440.1140.0044
200.0117430.1170.0043
21=Is CB list selection result of "ha" listIs Dependent ComboBox15 for "ha" & range0.0120420.1200.0042
220.0122410.1220.0041
230.0125400.1250.0040
24=Is CB list selection result of "m2" listIs Dependent ComboBox16 for"m2" & range 0.0129390.1290.0039
250.0132380.1320.0038
260.0136370.1360.0037
270.0139360.1390.0036
280.0143350.1430.0035
290.0148340.1480.0034
300.0152330.1520.0033
Sheet1
 
Upvote 0
Hi Jeff
In testing: "IF" I hadn’t just substituted "Private Sub Worksheet_Change (ByVal Target As Range)"
for "Private Sub ComboBox15_Change (ByVal Target As Range)", instead of using "Private Sub ComboBox15_Change()" then your advice would have resulted in the desired outcome sooner!!

If it’s not pushing the boundaries of Forum rules???
Can I ask a connected BUT supplementary question?
Based on my understanding, if you want/need multiple “Change Events” on the same sheet you need to have the
1st change event set as “Sub Worksheet_Change” and then sub OUT the End Sub of the 1st event and then sub OUT the “Sub Worksheet_Change” of the 2nd change event and its "End Sub", so on and so on.
Somewhat silly question but want to clarify??
Do the same rules apply if you use multiple Combobox changes??
Silly in as much as “ComboBox15_Change would be specific to CB15 and NOT to CB16, so NOT a generic change event as in “Worksheet_Change”????
 
Upvote 0
Based on my understanding, if you want/need multiple “Change Events” on the same sheet you need to have the
1st change event set as “Sub Worksheet_Change” and then sub OUT the End Sub of the 1st event and then sub OUT the “Sub Worksheet_Change” of the 2nd change event and its "End Sub", so on and so on.
Somewhat silly question but want to clarify??
I do not understand what you are trying to describe here, particularly the phrase "sub OUT the End Sub of the 1st event" which means nothing to me.

I am going to overexplain, and I hope the answer to your question is in here somewhere. Each worksheet may have only a single Sub Worksheet_Change. That Sub is called by the VBA runtime in a process known as a "callback". Normally you can write a sub and call it from somewhere else in your code. With a callback, you write a sub and the system calls it at a defined time. In this case, Worksheet_Change is called whenever the user changes the content of a cell in that worksheet. If there are multiple conditions you want to check for (for example, user types FINISHED into B4, or types STARTED into C3) you have to have logic in the sub to check which cell changed and check all the desired conditions. Worksheet_Change has one argument Target which is the worksheet range that changed.

Do the same rules apply if you use multiple Combobox changes??
Silly in as much as “ComboBox15_Change would be specific to CB15 and NOT to CB16, so NOT a generic change event as in “Worksheet_Change”????
Each control has to have its own change event Sub. However, in some cases, they all do the same thing, so they could all call a common Sub to do it. There is a technique for one sub to handle the change event for any type of control, like you have 12 checkboxes, one for each month, and they all do the same thing for their own month. This technique involves creating a Class so is not in the VBA 101 course.
 
Upvote 0
Thank for the detailed explanation, I’ve not been able to fully digest it, but still trying to.

But to clarify what I meant by “Sub OUT”.
Suppose we only have one change event on Sheet1, code would be something like:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
‘ Code to change item 1 here
End Sub
But if you want 2 or more change events on Sheet1; what I mean by "Sub out", would be something like:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
‘ Code to change item 1 here
           ‘End Sub of 1st Change event is NOT put in/used
           ‘Private Sub Worksheet_Change(ByVal Target As Range) of 2nd Change event is is NOT put in/used
‘ Code to change item 2 here
End Sub
So for 2 or more Change events on same sheet, actual code would be this;
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
‘ Code to change item 1 here
‘ Code to change item 2 here
End Sub
Hope that has cleared things up
Many thanks for your help
Julhs
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

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