Consulting

Results 1 to 5 of 5

Thread: Using a variable to call either a form or a sheet

  1. #1
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location

    Using a variable to call either a form or a sheet

    Hi

    Is it possible to do this?

        If Left(W, 3) = "frm" Then
            W.Show
        Else
            W.Activate
        End If
    I would like to send the user back to whence they came, which could be another sheet of a user form. All my UF's start with "frm" so I thought that the above could work. It doesn't!

    Any ideas?

    Thanks

    Paul Ked
    Semper in excretia sumus; solum profundum variat.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Nothing is ever that easy in Excel


    test() does a userform, and test2() does a worksheet


    Only difference is what W is set to


    The only gotcha is a UF must be loaded to be in the UserForms collection (based a 0)


    Option Explicit
    Sub test()
        Dim W As String
        Dim i As Long
        
        W = "frmOne"
        
        Load frmOne '   MUST be loaded
    
        If Left(W, 3) = "frm" Then
            For i = 0 To UserForms.Count - 1    '   start at 0
                If UserForms(i).Name = W Then UserForms(i).Show
            Next I
        Else
            Worksheets(W).Activate
        End If
    End Sub
    
    
    
    Sub test2()
        Dim W As String
        Dim i As Long
        Dim oUF As Object
        
        W = "Sheet1"
        
        Load frmOne
    
        If Left(W, 3) = "frm" Then
            For i = 1 To UserForms.Count
                If UserForms(i - 1).Name = W Then UserForms(i - 1).Show
            Next I
        Else
            Worksheets(W).Activate
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    That's great Paul, thanks
    Semper in excretia sumus; solum profundum variat.

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    How are you assigning a value to W? Assuming it's an Object variable, you can simply test its type using either Typename or TypeOf.
    Be as you wish to seem

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    How are you triggering the "Send User Back" event?

    ThisWorkbook Code:
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Set CurrentObject = Sh
    End Sub
    USerForm(s) Code:
    Private Sub UserForm_Activate()
    Set CurrentObject = Me
    End Sub
    Module Code:
    Public PreviousObject As Object
    Public CurrentObject As Object
    
    Public Sub SendBack()
    Dim W As Object
    Set W = CurrentObject
    
    If PreviousObject Is Null Then Exit sub 
    
     If PreviousObject Is UserForm Then '??? How to test? 
      PreviousObject.Load
     Else: PreviousObject.Activate 'Is Worksheet
     End If
     
    Set PreviousObject = W
    
    End Sub
    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

Posting Permissions

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