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:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

RickXL

MrExcel MVP
Joined
Sep 9, 2013
Messages
4,328
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
6,001
Office Version
  1. 365
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
6,001
Office Version
  1. 365
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
6,001
Office Version
  1. 365
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
6,001
Office Version
  1. 365
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,145
Messages
5,835,661
Members
430,373
Latest member
swartzfeger

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