Link to home
Start Free TrialLog in
Avatar of mathes
mathes

asked on

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?

Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

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

HAGD:O)Bruintje
Avatar of mathes
mathes

ASKER

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
Avatar of mathes

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of mathes

ASKER

Hi there,

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