如何使用VBA从Excel用户窗体中读取复选框的值 [英] How to use VBA to read the values of a checkbox from an Excel userform

查看:4747
本文介绍了如何使用VBA从Excel用户窗体中读取复选框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含两个复选框的用户窗体。我想能够做不同的事情,取决于每个框是否选中或取消选中。但是,似乎不管我做什么,它总是会告诉我复选框的原始值(false和false)。下面是点击CommandButton1的代码:

  Private Sub CommandButton1_Click()

ReadData

End Sub

此处为ReadData:

  Sub ReadData()

Dim myForm As UserForm
设置myForm = UserForms.Add(ComplaintEntryForm)

Debug.Print(myForm!CheckBox1.Name)
Debug.Print(myForm!CheckBox1.Value)
Debug.Print(myForm!CheckBox2.Name)
Debug.Print !CheckBox2.Value)

End Sub

无论如何立即窗口总是显示:

  VBA.UserForms.Add(ComplaintEntryForm)显示
CheckBox1
False
CheckBox2
False

解决方案

尝试使用此方法加载和显示表单(这在正常模块中):

  Sub main()

Dim myForm作为ComplaintEntryForm

设置myForm =新的ComplaintEntryForm
myForm.Show
设置myForm =无

结束子



在UserForm自己的模块中,添加以下内容:

  Private Sub CheckBox1_Change()

readData

End Sub

Private Sub CheckBox2_Change()

readData

End Sub

Private Sub UserForm_Initialize()

Me.CheckBox1.Value = True
Me.CheckBox2.Value = False

End Sub

Private Sub readData()

Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value

End Sub

我已经将初始化事件中的两个复选框初始化为特定值。这意味着我们确定表单将在


中开始的状态

I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:

Private Sub CommandButton1_Click()

ReadData

End Sub

And here ReadData:

Sub ReadData()

Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")

Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)

End Sub

No matter how the boxes are checked, the immediate window always shows this:

VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False

I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.

解决方案

Try this method to load and show the form (this goes in a normal module):

Sub main()

Dim myForm As ComplaintEntryForm

Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing

End Sub

In the UserForm's own module, add the following:

Private Sub CheckBox1_Change()

readData

End Sub

Private Sub CheckBox2_Change()

readData

End Sub

Private Sub UserForm_Initialize()

Me.CheckBox1.Value = True
Me.CheckBox2.Value = False

End Sub

Private Sub readData()

Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value

End Sub

I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in

这篇关于如何使用VBA从Excel用户窗体中读取复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆