Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a form on initialize throws error

Tags:

excel

vba

I'm trying to make a standardized process for updating a particular worksheet. I want no user control except for the functions I give them. To do that I have locked sheets and then forms that load with certain macros. One form is designed to remove data from the sheet. It works fine as written and tested, but I've tried to update it so that if you open it without any relevant data to remove, it spits out a dialogue box and then uses Unload Me to close the form. This closes the form but then excel throws an error:

Run-time error '91': Object variable or With block variable not set

The form is loaded from a module that only has the one line:

MyForm.Show

This is where excel is throwing the error from. On initialization of the form, a combobox is filled with values based on the data in the sheet. If the combobox is empty after loading, the form is supposed to throw the dialogue box and then close.

If ComboBox.ListCount = 0 Then
    MsgBox "No Data"
    Unload Me
End If

How can I perform the check on load without having the error thrown from the Module?

like image 568
vrcaldwellv Avatar asked Dec 14 '22 18:12

vrcaldwellv


1 Answers

This doesn't actually answer your question. But what I suggest is do the checking in your module code before you actually load the form. Something like:

Sub LoadForm()
    If Sheets("Sheet1").Range("A1") = "" Then '<~~ your condition here
        MsgBox "No Data"
    Else
        MyForm.Show
    End If
End Sub
like image 94
L42 Avatar answered Dec 26 '22 20:12

L42