Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel error, stops running macro

Tags:

excel

vba

I am experiencing an odd bug on Excel. I have a macro that shows a non-modal userform when I press CTRL+m (Macro shortcut). Every once in a while, and it's not that frequent (Shows up once or twice during the day, I use the macro every 5 minutes or so), Excel won't run the macro, won't show the userform and will just beep (as in "mistake, cannot proceed executing code").

I went into the Macro window to try to press "Run" and manually execute, but all buttons are disabled, except for "Create". If you click it, it says the macro name is not valid. As you can see in the screenshot below, the name of the macro shows the instance where the code is (Sheet1 of the workbook).

Sometimes it can be fixed by saving the workbook and just re-trying, but sometimes it doesn't; when it doesn't, I run a different macro (by double clicking a specific column) that shows a modal userform, and executing its code. Then my first macro returns to normal.

Any help will be very much appreciated.

Macro run window

Edit: Adding the code as requested in the comments

Sub ShowCommentWindow()
    Dim myCell As Range
    Dim companyColumn As Long
    Dim wbk as Workbook
    Dim company as String
    Dim phone as Long

    Set wbk = ActiveWorkbook

For Each myCell In wbk.Worksheets(1).Range("A1:Q1")

   If myCell.Text = "Company" Then
        companyColumn = myCell.Column
        company = ActiveCell.Text
        phone = ActiveCell.Offset(0, 4).Value
        Exit For
    End If
Next myCell

If ActiveCell.Column = companyColumn Then

    If EmailForm.Visible Then
        GoTo ExitProc
    Else
        If Not ActiveCell.Row < 4 Then
            ActiveWindow.ScrollRow = ActiveCell.Row - 3
        Else
            ActiveWindow.ScrollRow = ActiveCell.Row
        End If
        If CommentWindow.Visible Then
            CommentWindow.AddButton.SetFocus
            CommentWindow.CommentBox.SetFocus
            Exit Sub
        Else
            CommentWindow.Show
            ManageComments
            AddComment
        End If
    End If
End If

ExitProc:
End Sub

Edit2: Posting more code, for QueryClose:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

Dim myCell As Range
Dim isCompany As String

    If Not CommentWindow.CommentBox.Text = CommentWindow.TextCopy.Text Then
        saveConf = MsgBox("Changes have not been saved yet. Do you want to save?", vbExclamation + vbYesNoCancel + vbDefaultButton2, "Save changes?")
        If saveConf = vbYes Then
            Call SaveComment
            GoTo ExitProc
        ElseIf saveConf = vbCancel Then
            changed = True
            Cancel = 1
            CommentWindow.AddButton.SetFocus
            CommentWindow.CommentBox.SetFocus
            'CommentWindow.CommentBox.Text = CommentWindow.TextCopy.Text
        Else
            CommentWindow.TextCopy.Text = CommentWindow.CommentBox.Text
            GoTo ExitProc
        End If
    Else
        If Not changed = True Then
            GoTo ExitProc
        End If
    End If

ExitProc:
End Sub
like image 604
Andres Felipe Martinez Avatar asked Oct 30 '22 08:10

Andres Felipe Martinez


1 Answers

Seems like the issue is not unloading the forms from ( Unload(UserForm) )
This leads to a memory leak.
Even the official documentation -this refers to Access, but, should behave the same for Excel (there's no Form object or userform documentation there)- state the Lifecycle is Unload->Deactivate->Close, and this should happen when you close the userform as well, daily usage has shown that Unload if not stated may not be triggered when closing the userform.
The lifecycle is not that strictly monitored sometimes, but, that may lead to memory leaks and strange behaviors, always when working with objects you shouldn't rely that garbage collector will clean them if not specified. Probably adding something to confirm that terminate is being correctly handled will be helpful.
EDIT
If you're having problems remembering the unload -or still having problems with memory-, it will be a good practice to do the following:

Sub MyMainProcess()
Dim myform As UserForm1: Set myform = UserForm1 'this is your UserForm name
myform.Show
'my stuff needed...
Unload myform
Set myform = Nothing
End Sub

Unload and Nothing to clean as much as possible with coding

like image 76
Sgdva Avatar answered Nov 15 '22 05:11

Sgdva