excel - 从用户表单返回一个值

标签 excel vba userform

我正在尝试将值从用户窗体返回到另一个宏。

下面是我想要返回值 intMonth 的一段代码示例:

sub comparison()
    UserForm1.Show
end sub

然后我有用户表单代码:

Private Sub initialize()
    OptionButton1 = False
End Sub

Private Sub OptionButton1_Click()
    intMonth = 1
    Me.Hide
End Sub

如何将 1intMonth 值返回到原来的 comparison() 函数?

最佳答案

这是一个最小的示例,但应该有所帮助。

screenshot of UserForm

在用户表单中:

Option Explicit
Option Base 0

Public intMonth As Long    ' <-- the variable that will hold your output

Private Sub initialize()
    OptionButton1 = False
    intMonth = 0
End Sub

Private Sub CommandButton1_Click()  ' OK button
    Me.Hide
End Sub

Private Sub OptionButton1_Click()
    intMonth = 1    '<-- set the value corresponding to the selected radio button
End Sub

Private Sub OptionButton2_Click()
    intMonth = 2
End Sub

在模块或ThisWorkbook中:

Option Explicit
Option Base 0

Sub comparison()
    UserForm1.Show
    MsgBox CStr(UserForm1.intMonth)   ' <-- retrieve the value
End Sub

关于excel - 从用户表单返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51952304/

相关文章:

excel - 如何让 VBA 代码等待 vbModeless 用户窗体关闭

vba - 获取Excel INDEX函数找到的值的单元格引用

excel - 如何在Excel中查找倒数第二个非空白值

python - 使用 XLwings 将变量从 Excel 传递到 Python

vba - Excel VBA 中的循环单词匹配函数

VBA:在后台计算工作表,同时消息用户窗体处于事件状态

vba - 用户窗体中的全局变量

vba - 可以将变量存储在无法通过 Excel 访问的 Excel 文件中吗

function - 自定义查找功能

excel - 将所有 VBA 代码从工作簿复制到另一个工作簿