简体   繁体   中英

vba: userform functions

I have a sub that is activated from a button on a userform. The basic procedure from the click is

1) Run my sub based off of user inputs

2) Select results sheet

3) Display my results

4) Unload my userform

I've run into a problem because I want to try and put bounds on an user input value and if the user inputs something out of the range a message box will pop up notifying them of the range. I've been able to accomplish this simple task through the use of an if/then loop. After the user exits out of the message box I want to keep the userform displayed along with the original user inputs and allow the user to change their input. But currently after the user clicks 'ok' on the message box, my click sub continues its procedure and unloads my userform and selects my results worksheets. Is there a simple one line code that I can put after my msgbox state to preserve the userform instead of making the user re-enter their values?

EDIT - The general gist of my code is as follows:

Private Sub CommandButton1_Click()
    PropertySearch.Search
    ActiveSheet.Name = "SearchResult"
    Cells(1, 1).Select
    Unload ILsearch
End Sub

Sub Search()
    If (TextBox1 And TextBox2 <= 8) And (TextBox1 And TextBox2 > 0) Then
    '
    'Performs my desired function
    '
    Else: MsgBox ("Database only includes ionic liquids with a maximum of 8 carbons in cation")
    End If

I would turn Search into a function which returns true or false if the input is within bounds or not:

Function Search() AS Boolean

   If (TextBox1 And TextBox2 <= 8) And (TextBox1 And TextBox2 > 0) Then
      Search = True
   Else
      Search = False
   EndIf

End Function

then you just exit the sub if input does not meet your bounds:

Private Sub CommandButton1_Click()

    If(Not PropertySearch.Search) Then
       MsgBox("your error message here")
       Exit Sub
    EndIf

    ' rest of the routine

End Sub

There are about a thousand and one ways around this - the question is what behavior do you want the form to have?

Do you want it to self close after a certain amount of time? Check out this example of timer 1

Or look into Application.Ontime

Application.OnTime Now + TimeValue("00:00:10"), "unloadForm"

Where "unloadForm" is a sub in a normal module

Sub unloadForm()
    Unload ILsearch
End Sub

Do you want to add a close form button?

Private Sub CommandButton1_Click ()
    Unload Me
End Sub

Do you want the user to close the form manually with the red X in the top corner? Simply delete the line with Unload

Do you want to display a modal popup window that freezes Excel until closed and then your form unloads? Try addging MsgBox "Hello" before you unload the form.

And many many many more!

For example, I have several forms that use keyboard events. Escape can clear all fields and hide/unload the form while Enter does the same thing but also write the values to a table, Delete only clears the active control and doesn't hide the form, up and down arrows cycle through forms hiding the current and showing the previous/next while left and right function like tab/shift + tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM