Compile error despite no unmatched Sub declarations

Seren

New Member
Joined
Jul 21, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm currently putting together a simple data reporting tool. Part of it is a UserForm for selecting parameters for applying filtering to the data. The UserForm has a ComboBox (loaded_cols_combo_box), three TextBoxes(cutoff_fraction_textbox, skip_n_textbox, max_range_sz_textbox), and two CommandButtons(confirm_settings_button, no_filter_button). As far as I can remember, I haven't made any recent alterations to the VBA for the UserForm and it was working as intended until very recently.

The issue is caused in two separate situations. When the X button in the upper-right is pressed it causes a Compile error with the message 'Only comments may appear after End Sub, End Function, or End Property'. And when the no_filter_button is pressed giving a compile error with the message 'Expected End Sub' and causing the application to get stuck with an inactive UserForm in the way so it can only be closed with task manager.

Oddly, adding an additional unmatched 'End Sub' before the final line of the no_filter_button_Click() sub fixes the issue with the no_filter_button so it functions as intended.

It's entirely possible I'm doing something really stupid/obvious here, but I've been staring at the VBA on and off since yesterday morning and can't work out what's wrong. Is there any way that code from other modules, etc could cause a compilation error in this UserForm? I would hope not.

The VBA for the UserForm is as follows:
VBA Code:
Public valid_selection As Boolean
Public Function check_valid_inputs() As Boolean
    Dim su_changed As Boolean: su_changed = False
    If Application.ScreenUpdating Then
        su_changed = True
        Application.ScreenUpdating = False
    End If
    
    Dim valid_column As Boolean
    valid_column = ("" <> loaded_cols_combo_box.Value)

    Dim valid_cutoff As Boolean
    Dim cutoff As Double
    cutoff = CDbl(cutoff_fraction_textbox.Value)
    valid_cutoff = (0 <= cutoff And cutoff <= 1)
    
    Dim valid_skip_n As Boolean
    Dim skip_n As Long
    skip_n = CLng(skip_n_textbox.Value)
    valid_skip_n = (skip_n > 0)
    
    Dim valid_range_sz As Boolean
    Dim range_sz As Long
    range_sz = CLng(max_range_sz_textbox.Value)
    valid_range_sz = (range_sz >= 0)

    valid_selection = valid_column And _
                      valid_cutoff And _
                      valid_skip_n And _
                      valid_range_sz
    
    check_valid_inputs = valid_selection
    
    If su_changed Then
        Application.ScreenUpdating = True
    End If
End Function
Public Sub clear_inputs()
    Dim su_changed As Boolean: su_changed = False
    If Application.ScreenUpdating Then
        su_changed = True
        Application.ScreenUpdating = False
    End If

    ' Clear selections
    loaded_cols_combo_box.clear
    cutoff_fraction_textbox.Value = ""
    skip_n_textbox.Value = ""
    max_range_sz_textbox.Value = ""
    valid_selection = False

    If su_changed Then
        Application.ScreenUpdating = True
    End If
End Sub
Private Sub confirm_settings_button_Click()
    Dim su_changed As Boolean: su_changed = False
    If Application.ScreenUpdating Then
        su_changed = True
        Application.ScreenUpdating = False
    End If

    If check_valid_inputs = True Then
        FilterSelect.Hide
    End If

    If su_changed Then
        Application.ScreenUpdating = True
    End If
End Sub
Private Sub no_filter_button_Click()
    Dim su_changed As Boolean: su_changed = False
    If Application.ScreenUpdating Then
        su_changed = True
        Application.ScreenUpdating = False
    End If

    If loaded_cols_combo_box.ListCount <> 0 Then
        loaded_cols_combo_box.Value = loaded_cols_combo_box.List(0)
    Else
        loaded_cols_combo_box.Value = "__NoFilter"
    End If
    cutoff_fraction_textbox.Value = 0
    skip_n_textbox = 1
    max_range_sz_textbox.Value = 1
    valid_selection = True

    FilterSelect.Hide

    If su_changed Then
        Application.ScreenUpdating = True
    End If
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Dim su_changed As Boolean: su_changed = False
    If Application.ScreenUpdating Then
        su_changed = True
        Application.ScreenUpdating = False
    End If

    ' Clear form
    clear_inputs

    ' Cancel loading
    valid_selection = False

    If su_changed Then
        Application.ScreenUpdating = True
    End If
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I can't see anything wrong with the code either. I recall having had something similar happen in the past. I copied all the sheets to a new workbook (using right-click on the sheet tabs). Then copied the VBA modules using copy/paste and the userform by creating new userform and pasting the form module code. That is the worst part, having to create the userform again, with all the control properties.


Alternatively you can try check when things go wrong: comment out all the code in each of the functions. Then uncomment bit by bit.
 
Upvote 0
Thanks for the reply, it seems to have resolved itself now. Not sure if I did something specific to fix it, but I'm not gonna push my luck and mess with it!
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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