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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

sykes

Well-known Member
Joined
May 1, 2002
Messages
1,701
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,701
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,701
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,701
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,701
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,102,871
Messages
5,489,402
Members
407,687
Latest member
NeoSez

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top