Need help with VBA Userform

mhessnm

Board Regular
Joined
Apr 12, 2019
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hello, I can't figure out what is going on. I am a relatively new VBA programmer. Long story short, I have an input userform called by a macro attached to a button on the spreadsheet. It works perfectly. I also have a userform to allow someone to sort over multiple spreadsheets at once, and it works perfectly. I pretty much copied the macro that works with the input form because other than what they do, there wasn't much difference in how they were called. However, I'm running into an error that I can't understand. The form appears as it's supposed to, and runs through the sort based on what is clicked. It then unloads but I end up with a "Runtime error 91: Object variable or With block variable not set." I have marked in red where it gets hung up. For reasons I don't understand, after the user form runs and unloads, it comes back to that line in the calling subroutine and gives the error. It's frustrating me, particularly it seems to work fine for the other form, the input form, I have.

Here is the code for the calling macro attached to a button:

Sub Sort_Form()


'Call sort form


Call frmRearrange.UserForm_Initialize


'Allows for closure with X box


If closemode = vbFormControlMenu Then
Unload frmRearrange
Exit Sub
End If


End Sub

And here is the code for the sort userform:

Sub UserForm_Initialize()


Me.StartUpPosition = 0
Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)

frmRearrange.Show


End Sub

(This sub repeated, only changing the option name and sorting criteria)
Private Sub optSortEnrollment_Click()


Dim ws_names As Variant
Dim ws As Variant
Dim LastRow As Long


ws_names = Array("Address_Needs", "Services Provided", "Crisis_Treatment", "Services Leveraged", "Discharge")


For Each ws In ws_names
LastRow = Worksheets(ws).UsedRange.Rows.Count
Worksheets(ws).Range("A2:BB" & LastRow).Sort Key1:=Worksheets(ws).Range("BB2"), Order1:=xlAscending, Header:=xlYes
Next ws

LastRow = Worksheets("Administrative").UsedRange.Rows.Count
Worksheets("Administrative").Range("A2:BB" & LastRow).Sort Key1:=Worksheets("Administrative").Range("E2"), Order1:=xlAscending, Header:=xlYes


Unload Me


End Sub

**************

Any help for this newbie would be appreciated! Thank you in advance!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I think I can see your problem...

You need to know that any userform has allied to it, a "userform_initialize" event - i.e. a bit of code reserved for it to run, if the UserForm's started. If the form's started, it looks at it's own _initialize event, to see if there's anything that needs running, before it's displayed to the user. This happens automatically when the form's called with it's ".show" procedure.
If you, as the coder, want anything to happen before the form's displayed (like one of its textboxes setting to the value of a cell etc) then you put the code in here. To get to this built-in event, go to your form in the VBA browser (right-clicking it will do) and select "View code." On the code pane - normally on the RH side - at the top left you'll see the drop down with your userform's name, and from the top right dropdown, select "_initialize" which displays the start and finish lines of the form's _initialize event. Any code you put in here runs automatically, before the form's displayed, and after it's called.

In your code, you're actually calling the initialize event - so it's firing twice.

To call your userform, just use:
Code:
"your_form_name.Show"
The initialize code runs, then your form is displayed to the user.

This should work:
Code:
Sub Sort_Form()


'Call sort form


frmRearrange.Show


'Allows for closure with X box


If closemode = vbFormControlMenu Then
Unload frmRearrange
Exit Sub
End If


End Sub
'And here is the code for the sort userform's initialize event( which will run by itself):
Code:
Sub UserForm_Initialize()

Me.StartUpPosition = 0
Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)

End Sub
 
Upvote 0
Solution
Ah...thank you for explaining that to me, Sykes. That makes sense - as I couldn't figure out why it was coming back to the userform_initialize command and doing it again. I'll give it a try! Thank you for looking at it and the suggestions!
 
Upvote 0
Pleasure - let us know if that was the issue - always good for others with a similar problem, to be able to easily find a solution.
 
Upvote 0
Hi Sykes, I just tried it and it worked perfectly. Thank you again!
 
Upvote 0
... by the way...
Where you have an object to which your code repeatedly refers, consider using the With...EndWith statements. If you know you're gong to be making repetition, it cuts down your typing, and it also makes for easier reading of the code, as far as brevity is concerned.

your code
Code:
Sub UserForm_Initialize()

Me.StartUpPosition = 0
Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)

End Sub
could be written
Code:
Sub UserForm_Initialize()
    With Me
    .StartUpPosition = 0
        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    End With
End Sub
This isn't a particularly good example, as the "Me" keyword's short, but if you consider a worksheet object like:
Code:
Sheets("my_extrapolation worksheet")
... you can imagine typing than twenty times in one bit of a procedure, and it soon gets tediious!!
Just one friendly tip, out of about a million...
 
Upvote 0
Lovely job! Thanks for letting everyone know - and for the feedback.
 
Upvote 0
Thanks for the extra tip - I'm always trying to cut down on the wordiness of my programming to make it efficient, but being relatively new still struggle with that. It's really like learning a new language - you want to keep it brief but don't really have the words and fumble around a bit until someone corrects you. Thanks for the helpful correcting!
 
Upvote 0
You're very welcome; been there, done that - when I look back at my early attempts, it's really embarrassing; still a bit "schoolboy" when compared to that of some of the Top Neddies on this great site!
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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