Need help with VBA Userform

mhessnm

New Member
Joined
Apr 12, 2019
Messages
25
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!
 

Some videos you may like

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
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
 

mhessnm

New Member
Joined
Apr 12, 2019
Messages
25
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!
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
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.
 

mhessnm

New Member
Joined
Apr 12, 2019
Messages
25
Hi Sykes, I just tried it and it worked perfectly. Thank you again!
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
... 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...
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
Lovely job! Thanks for letting everyone know - and for the feedback.
 

mhessnm

New Member
Joined
Apr 12, 2019
Messages
25
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!
 

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,660
Office Version
365
Platform
Windows
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,101,903
Messages
5,483,622
Members
407,400
Latest member
Rakeforms

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top