VBA:用户窗体函数

发布于 2024-11-18 05:08:24 字数 798 浏览 3 评论 0原文

我有一个通过用户表单上的按钮激活的子程序。单击的基本过程是

1) 根据用户输入运行我的子程序

2) 选择结果表

3) 显示我的结果

4) 卸载我的用户表单

我遇到了问题,因为我想尝试对用户进行限制输入值,如果用户输入超出范围的内容,则会弹出消息框,通知他们范围。我已经能够通过使用 if/then 循环来完成这个简单的任务。用户退出消息框后,我希望保留用户窗体与原始用户输入一起显示,并允许用户更改其输入。但目前,在用户单击消息框中的“确定”后,我的单击子程序将继续其过程并卸载我的用户表单并选择我的结果工作表。是否有一个简单的一行代码可以放在 msgbox 状态之后以保留用户表单,而不是让用户重新输入其值?

编辑-我的代码的一般要点如下:

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-11-25 05:08:24

我会将 Search 转换为一个函数,如果输入是否在边界内,该函数将返回 true 或 false:

Function Search() AS Boolean

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

End Function

然后,如果输入不符合您的边界,您只需退出子程序:

Private Sub CommandButton1_Click()

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

    ' rest of the routine

End Sub

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
明媚如初 2024-11-25 05:08:24

大约有一千零一种方法可以解决这个问题 - 问题是您希望表单具有什么行为?

您希望它在一段时间后自动关闭吗?
查看计时器的示例 1

或者查看 Application.Ontime

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

其中“unloadForm”是普通模块中的子项

Sub unloadForm()
    Unload ILsearch
End Sub

您想添加关闭表单按钮吗?

Private Sub CommandButton1_Click ()
    Unload Me
End Sub

您是否希望用户通过右上角的红色 X 手动关闭表单?只需使用 Unload 删除该行

是否要显示一个模式弹出窗口,冻结 Excel 直到关闭,然后卸载表单?在卸载表单之前尝试添加 MsgBox "Hello"

还有很多很多很多!

例如,我有几种使用键盘事件的表单。 Escape 可以清除所有字段并隐藏/卸载表单,而 Enter 执行相同的操作,但还将值写入表中,Delete 只清除活动控件而不隐藏表单,向上和向下箭头循环浏览隐藏当前控件的表单并显示上一个/下一个,同时左右功能如 tab/shift + tab。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文