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!
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!