【问题标题】:VBA: How to pass the contents from multi-checkbox (created in userform) to moduleVBA:如何将内容从多复选框(在用户表单中创建)传递到模块
【发布时间】:2019-05-12 15:07:13
【问题描述】:

我想将 multicheckbox 中的内容(在用户表单中创建)传递给模块以供进一步使用。 代码中的这个函数是:

在模块中:

 Public pass1 As Boolean
 Public pass2 As Boolean
 Public pass3 As Boolean
 Public pass4 As Boolean
Public Sub MultiCheckBoxes()
    Dim varArraySelected As Variant
    Dim ivar As Long
    varArraySelected = Array()

    UserForm1.Show

    ivar = 0
    If pass1 = True Then
        ReDim Preserve varArraySelected(0 To ivar)
        varArraySelected(ivar) = "Fase 1"
        ivar = ivar + 1 'Advance the counter to next array item

    End If
    If pass2 = True Then
            ReDim Preserve varArraySelected(0 To ivar) 'Reset the array dimension on each iteration of loop
            varArraySelected(ivar) = "Fase 2" 'Add value in Column B to Array
            ivar = ivar + 1 'Advance the counter to next array item

    End If
    If pass3 = True Then
            ReDim Preserve varArraySelected(0 To ivar) 'Reset the array dimension on each iteration of loop
            varArraySelected(ivar) = "Fase 3" 'Add value in Column B to Array
            ivar = ivar + 1 'Advance the counter to next array item

    End If
    If pass4 = True Then
            ReDim Preserve varArraySelected(0 To ivar) 'Reset the array dimension on each iteration of loop
            varArraySelected(ivar) = "Fase 4"  'Add value in Column B to Array

    End If
    Unload UserForm1

End Sub

在用户窗体中:

Option Explicit    
Private Sub CheckBox1_Click()    
End Sub
Private Sub CheckBox2_Click()      
End Sub
Private Sub CheckBox3_Click()  
End Sub
Private Sub CheckBox4_Click()    
End Sub

Private Sub CommandButton1_Click()        
    pass1 = UserForm1.CheckBox1.Value
    pass2 = UserForm1.CheckBox2.Value    
    pass3 = UserForm1.CheckBox3.Value  
    pass4 = UserForm1.CheckBox4.Value
    Unload UserForm1
End Sub

这里 'Fase 1',..., 'Fase 4' 是在 UserForm1 中创建的四个复选框中的文本内容。

运行 VBA 模块时,我收到错误“检测到不明确的名称:pass1”。如何解决问题?

提前致谢!

【问题讨论】:

  • 您在工作簿的其他任何地方是否有第二个 pass1 声明?

标签: excel vba checkbox parameter-passing userform


【解决方案1】:

将您的子更改为:

Public Sub MultiCheckBoxes(pass1 As Boolean, pass2 As Boolean, pass3 As Boolean,pass4 As Boolean)

然后这样称呼它:

MultiCheckBoxes(UserForm1.CheckBox1.Value, UserForm1.CheckBox2.Value, UserForm1.CheckBox3.Value, UserForm1.CheckBox4.Value)

【讨论】:

  • 为我工作!非常感谢弗兰克!
猜你喜欢
  • 2013-12-11
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2015-03-01
  • 2015-05-07
  • 1970-01-01
  • 2018-04-24
  • 2023-02-01
相关资源
最近更新 更多