Need help with VBA Userform
Results 1 to 9 of 9

Thread: Need help with VBA Userform
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Apr 2019
    Location
    Albuquerque, NM
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Need help with VBA Userform

    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!

  2. #2
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Location
    Cornwall,England
    Posts
    1,560
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    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
    Sykes
    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.


    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

  3. #3
    New Member
    Join Date
    Apr 2019
    Location
    Albuquerque, NM
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    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!

  4. #4
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Location
    Cornwall,England
    Posts
    1,560
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    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.
    Sykes
    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.


    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

  5. #5
    New Member
    Join Date
    Apr 2019
    Location
    Albuquerque, NM
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    Hi Sykes, I just tried it and it worked perfectly. Thank you again!

  6. #6
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Location
    Cornwall,England
    Posts
    1,560
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    ... 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
    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.


    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

  7. #7
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Location
    Cornwall,England
    Posts
    1,560
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    Lovely job! Thanks for letting everyone know - and for the feedback.
    Sykes
    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.


    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

  8. #8
    New Member
    Join Date
    Apr 2019
    Location
    Albuquerque, NM
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    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!

  9. #9
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Location
    Cornwall,England
    Posts
    1,560
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Need help with VBA Userform

    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!
    Sykes
    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.


    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •