+ Reply to Thread
Results 1 to 2 of 2

userform return value

Hybrid View

gtmeloney userform return value 07-08-2009, 11:01 AM
Andy Pope Re: userform return value 07-08-2009, 11:26 AM
  1. #1
    Registered User
    Join Date
    05-05-2009
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2003
    Posts
    97

    userform return value

    I call a userform and need to determine whether the user clicked "OK" or "Cancel" on the form. Is there a way to do this without setting a global variable and checking it after the form is closed?

    I want to say something like:

     myInt = userForm1.run
    where "run" is a method in userForm1 that returns a different value for OK and Cancel. But I don't know how to make this happen.

    Thanks Andy!
    Last edited by gtmeloney; 07-08-2009 at 12:24 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: userform return value

    Here's how I do it for my userforms.

    standard code module
    Sub Test()
    
        Dim frmTest As UserForm1
        
        Set frmTest = New UserForm1
        
        If frmTest.UserCancel Then
            MsgBox "User cancelled"
        Else
            MsgBox "User closed as expected"
        End If
        
        Unload frmTest
        Set frmTest = Nothing
        
    End Sub
    Userform1 with 2 buttons
    code
    Private m_blnUserCancelled As Boolean
    Public Function UserCancel() As Boolean
    
        m_blnUserCancelled = True
        
        Me.Show
        
        UserCancel = m_blnUserCancelled
        Exit Function
        
    End Function
    
    Private Sub CommandButton1_Click()
        Me.Hide
    End Sub
    
    Private Sub CommandButton2_Click()
    
        m_blnUserCancelled = False
        Me.Hide
        
    End Sub
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1