Consulting

Results 1 to 5 of 5

Thread: Set statement

  1. #1

    Set statement

    I have this code place in the Private Sub UserForm_Initialize() procedure. I reference this code in a few other procedure in the same userform, so I decided to reduce the redundancy and place it in the initialization. Sometimes it works but i notice it would create error alert. If I place this code in the procedure of the error, it runs without any issues. Can someone explain why?

    Thanks


    Run-time error 91
    Object Variable or With block variable not set

    Set Wks = ActiveSheet

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Whenever possible, avoid referring to any "Active" object
    Set Wks = Sheets("Sheet1")
    It is possible to have an open UserForm, with no active sheets
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    You don't need an Object variable for an object you can refer to directly.
    Sheet1. in your code suffices.

  4. #4
    I would not declare a variable for ActiveSheet.

    If you want to declare a variable to be used by other procedures in the UserForm then place it at the top of the code module, not in any Procedure.

    Option Explicit
    
    Dim Wks As Worksheet
    
    Private Sub UserForm_Initialize()
    Set Wks = Sheet1
    End Sub
    Hope that helps.
    Get the free Excel based Toolbar or click here for free examples & Excel Consultancy

    RoyUK

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by av8tordude View Post
    I have this code place in the Private Sub UserForm_Initialize() procedure. I reference this code in a few other procedure in the same userform, so I decided to reduce the redundancy and place it in the initialization. Sometimes it works but i notice it would create error alert. If I place this code in the procedure of the error, it runs without any issues. Can someone explain why?

    Thanks
    Can you open your userform when there's different active sheets?

    e.g.

    If "Sheet1" is active then you want the UF to act on "Sheet1"

    If "Sheet2" is active then you want the UF to act on "Sheet2"

    etc.?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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