【问题标题】:Setting CheckBoxes from another userform in VBA在 VBA 中从另一个用户窗体设置复选框
【发布时间】:2016-07-06 10:50:50
【问题描述】:

我有一个用户表单,其中包含从 1 到 100 的多个复选框。我编写了一些非常简单的代码,因此当您提交表单时,它会创建一个二进制字符串,表示这 100 个复选框的状态,其中 0 为假1 为真。执行此操作的代码在这里:

Private Sub BusRulesSubmit_Click()
 Dim myBinaryString As String
 Dim nm As String
 Dim c As Control

 For busRuleIdx = 1 To 100
  nm = "CheckBox" & busRuleIdx
  Set c = Controls(nm)
  If c.Value = True Then
   myBinaryString = myBinaryString & "1"
  Else
   myBinaryString = myBinaryString & "0"
  End If
 Next

 msgBox myBinaryString
End Sub

我现在想从另一个表单打开这个用户表单,我有一个类似的二进制字符串,并使用这个字符串将复选框的值设置为适当的真或假。但是,我在设置控件时遇到了问题。代码在这里:

Private Sub populateBusRules()
 Dim c As Control
 Dim myBRBinary As String
 myBRBinary =    "000000000011100000000000000000000000000000000000000000000000000000000010000000000000000000000000000"

 For busRuleIdx = 1 To 100
  nm = "BusinessRules.CheckBox" & busRuleIdx
  Set c = Controls(nm)
  If Mid(myBRBinary, buRuleIdx - 1, 1) = 1 Then
   c.Value = True
  Else
   c.Value = False
  End If
 Next
End Sub

当我运行上面的代码时,我得到一个运行时错误“找不到指定的对象”,当要调试时,它会突出显示代码状态为“Set c = Controls(nm)”的这个问题 - 我可以看到它在第一轮循环中失败,即 nm = "BusinessRules.CheckBox1"

有趣的是,如果我运行代码“Set c = Controls(BusinessRules.CheckBox1)”,我不会遇到这样的问题。

任何帮助将不胜感激。

谢谢,

保罗。

【问题讨论】:

  • 您好,您对 VBA 的熟练程度如何?原始表单是否保持打开状态?
  • 如果没有同时打开表单,则需要全局变量。
  • 是的,表单同时打开 - 虽然我使用隐藏命令
  • 你能参考表格形成frm2.control.value=frm1.control.value吗?

标签: vba checkbox userform


【解决方案1】:

我认为BusinessRules 给了你这个问题。在 Controls 集合中,没有名为 "BusinessRules.CheckBox1" 的控件,在 BusinessRules.Controls 集合中只有一个名为 "CheckBox1" 的控件。假设上面的 cmets 中没有提到其他问题(例如在调用此之前关闭表单),那么这应该可以解决您的问题

【讨论】:

  • 这很有意义。所以这可以通过使用“Set c = BusinessRules.Controls(nm)”来解决
猜你喜欢
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 2018-08-29
  • 2015-01-17
  • 2017-04-03
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多