Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8012
  • Last Modified:

Excel, VBA; OnChange event of a ComboBox

Hi there,

is it possible to temporarily disable the OnChange event of a combobox?

I have a userform with a combobox. The combobox gets its content from the
column A of my spreadsheet.

During runtime of this macro the content of the column A changes,
and unfortunately the event OnChange is launched.

I tried to avoid this problem with a code like this:

Dim ComboStatus as Boolean

Private Sub cbo_Change()
    Dim SelectedThematics As String
    Dim c As Range

If ComboStatus = True Then

' some code here

End Sub 'cbo_Change()
Private Sub btnSaveChanges_Click()

' Button on the same userform, together with the combobox

ComboStatus = False

' Some code here

End Sub  ' Sub btnSaveChanges_Click()

Can you please tell me, why the OnChange event is executed, whenever I click on the CommandButton,
although I tried to avoid it by using the boolean variable by setting it to status = false ?

Is there another way how I can avoid the OnChange event of the combobox?

I already tried combobox.enabled = false and combobox.locked= true,
but this does not work either.

Have you any other suggestions how my problem can be solved?

  • 4
  • 3
1 Solution
Hello Mathes,

seems no other way then to temporarely disable the box but that would be no solution ....

why not use the click event if it's only to enable the user to make a choice out the box, then the change event is empty and can be raised any time

you handle the user action in the click or buttonup event of the box

mathesAuthor Commented:
Hi there,

thank you very much for your input.

The CommandButton does more than simply doing a selection.

After selecting from the Combobox, a dataset is copied from the spreadsheet, and displayed within several textedit controls of the userform.
Now the user can edit the dataset. When he has finished, he can click the CommandButton. Now the changes are written to the spreadsheet and the spreadsheet is updated and saved. This is the moment when unfortunately the OnChange event of the combobox is launched.

Is there a solution for my problem?
well as per above you do not need the onchange event to trigger the copy of the dataset

you can do this

Private Sub ComboBox1_Click()
'put the code in the onchangevent here
End Sub

and do not use the onchange event at all
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

mathesAuthor Commented:
Hi there,

The OnChange event is required.

After the userform has been loaded, I want that the user makes his choice. He selects the dataset to be modied by selecting one of the items of the combobox.

The CommandButton is only for saving the changes, after the user has edited the dataset, selected before. It is not the task of the CommandButton to select a dataset.
ok you can pickup a sample of what i meant .... the combobox HAS a click event of it's own so use that one INSTEAD of the onChange event


2 boxes one with only a different event implemented
Have a look into http://www.j-walk.com/ss/excel/tips/tip42.htm . If it suits you 'as is' you can use it for free, unlocking the code costs 20 USD.
mathesAuthor Commented:
Hi there,

thank you very much for your help. This is exactly what I was looking for.
:) thanks for the grade

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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