Odd VBA behaviour...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,710
Hi,

Can anyone spot an issue with the following?

Code:
MsgBox "Operation not possible at this time.", vbInformation, "Information"
.CheckBox1.Value = False

Basically, my code runs when CheckBox1 is ticked, but if there is an error in the code, the above error trapping kicks in (a message box appears and CheckBox1 is unticked). Things work as expected, but the MsgBox doesn't close when OK is first pressed - it has to be clicked twice for the window to shut.

Any ideas?

Thanks,

Matty
 
Last edited:

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

RickXL

MrExcel MVP
Joined
Sep 9, 2013
Messages
4,323
Hi,

My guess would be that the code you have not shown us is running the MsgBox code twice. It probably depends on how you have set up your error trapping (or event handling).
 

starl

Administrator
Joined
Aug 16, 2002
Messages
5,969
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
Have you stepped through your code? It's possible that the message box is appearing twice.
 

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,710
Hi All,

The code runs when the CheckBox is clicked. If I step through it manually, however, the behaviour described doesn't occur.

It's very strange. Maybe a bug?

Matty
 

starl

Administrator
Joined
Aug 16, 2002
Messages
5,969
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

if you have code running when the checkbox is clicked, then the code runs again when the value is changed to False.. if the error still exists, the message box will appear again..
of course, that is a guess without seeing the full code.
 

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,710
if you have code running when the checkbox is clicked, then the code runs again when the value is changed to False.. if the error still exists, the message box will appear again..
of course, that is a guess without seeing the full code.

I think you've hit the nail on the head with this. I don't have the code with me at the moment, but I'm pretty sure I've set EnableEvents to False, which I believed should have stopped this. Or am I missing something?

Thanks,

Matty
 

starl

Administrator
Joined
Aug 16, 2002
Messages
5,969
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

If you're talking about the click event on the checkbox - EnableEvents does not work on ActiveX controls
 

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,710
If you're talking about the click event on the checkbox - EnableEvents does not work on ActiveX controls

Thanks for confirming. Is there a way to get around this in that case?

Matty
 

starl

Administrator
Joined
Aug 16, 2002
Messages
5,969
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
You could set a boolean the first time the event is triggered so that when it gets triggered again, it skips the rest of the code.
 

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,710
Hi,

I have been reading about declaring Booleans and while I do understand the concept, I'm struggling to see how I could employ it to meet my needs.

Here's my code (happy for it to be scrutinised for any improvement, too!):

Code:
Private Sub FreezePanes_Click()

    Dim ColNum As Long
    Dim RowNum As Long
    Dim HorMid As Long
    Dim VerMid As Long
    
    Application.ScreenUpdating = False

    If FreezePanes.Value = False Then
        ActiveWindow.FreezePanes = False
    Else
        With Sheet1
            On Error GoTo Finish
            ColNum = Application.Match("Show", .Range("S6:ER6"), 0) + 18
            RowNum = Application.Match("Show", .Range("B136:B1844"), 0) + 134
            .Cells(RowNum, ColNum).Select
            Application.Goto reference:=ActiveCell, Scroll:=True
            With ActiveWindow
                HorMid = .VisibleRange.Rows.Count / 2
                VerMid = .VisibleRange.Columns.Count / 2
                .SmallScroll Up:=HorMid, ToLeft:=VerMid
                .FreezePanes = True
            End With
        End With
    End If
    
    Application.ScreenUpdating = True
    Exit Sub

Finish:
    MsgBox "You need to specify some data before using this feature.", vbInformation, "Information"
    
End Sub

Can you help?

Thanks,

Matty
 

Watch MrExcel Video

Forum statistics

Threads
1,122,518
Messages
5,596,629
Members
414,082
Latest member
sasmita

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
Top