Odd VBA behaviour...

Matty

Well-known Member
Joined
Feb 17, 2007
Messages
3,717
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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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).
 
Upvote 0
Have you stepped through your code? It's possible that the message box is appearing twice.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
If you're talking about the click event on the checkbox - EnableEvents does not work on ActiveX controls
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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