关于多页:除非所有页面都输入,否则禁用commandbuttom2

时间:2018-12-15 03:10:14

标签: excel vba textbox multipage

我在这里有一个代码,该代码将根据文本框中的值生成页面。

'Button accepting how many number of pages
Private Sub CommandButton1_Click()

RowChar = 70
MultiPage1.Pages.Clear

For i = 0 To TextBox1.Value - 1
    MultiPage1.Pages.Add
    MultiPage1.Pages(i).Caption = "Variable" & i + 1

    Call LabelPerPage

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "NameBox")
    With txtbx
        .Top = 20
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MinBox")
    With txtbx
        .Top = 50
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "LsbBox")
    With txtbx
        .Top = 20
        .Left = 300
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "Mataas")
    With txtbx
        .Top = 50
        .Left = 300
    End With

    If i = 0 Then
        FormulaString = "= C15"
    Else
        FormulaString = FormulaString & "  " & Chr(RowChar) & "15"
        RowChar = RowChar + 3
    End If

Next i
TextBox2.Value = FormulaString
End Sub

问题:如果每个页面内的所有文本框都为空,我想禁用commandbutton2(用于计算MINbox和MAxbox的按钮)。您有想法怎么办?谢谢。

3 个答案:

答案 0 :(得分:1)

虽然最好的方法和最简单的方法是验证@Excelosaurus回答的CommandButton2_Click中的点击,但我只是通过@Mathieu Guindon的回答 {{ 3}} 这种封装WithEvents MSForms控件的技术的全部功劳归功于@Mathieu Guindon

可以如下修改Userform1代码模块中的

Public handlers As VBA.Collection     ' added
Private Sub CommandButton1_Click()
RowChar = 70
MultiPage1.Pages.Clear

For i = 0 To TextBox1.Value - 1
    MultiPage1.Pages.Add
    MultiPage1.Pages(i).Caption = "Variable" & i + 1

    'Call LabelPerPage

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "NameBox")
    With txtbx
        .Top = 20
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "MinBox")
    With txtbx
        .Top = 50
        .Left = 100
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "LsbBox")
    With txtbx
        .Top = 20
        .Left = 300
    End With

    Set txtbx = UserForm1.MultiPage1.Pages(i).Controls.Add("Forms.TextBox.1", "Mataas")
    With txtbx
        .Top = 50
        .Left = 300
    End With

    If i = 0 Then
        FormulaString = "= C15"
    Else
        FormulaString = FormulaString & "  " & Chr(RowChar) & "15"
        RowChar = RowChar + 3
    End If

Next i
TextBox2.Value = FormulaString
CommandButton2.Enabled = False ' added
makeEvents                     ' added   
End Sub

Sub makeEvents()                 ' added             
    Set handlers = New VBA.Collection
    Dim cnt As MSForms.Control

    For i = 0 To UserForm1.MultiPage1.Pages.Count - 1
    For Each cnt In UserForm1.MultiPage1.Pages(i).Controls
        If TypeOf cnt Is MSForms.TextBox Then
            Dim textBoxHandler As DynamicTextBox
            Set textBoxHandler = New DynamicTextBox
            textBoxHandler.Initialize cnt
            handlers.Add textBoxHandler
        'Debug.Print cnt.Name & i & "Inited"
        End If
     Next cnt
     Next i
End Sub

然后将一个新的类模块添加到您的项目中,将其命名为DynamicTextBox

Option Explicit
Private WithEvents encapsulated As MSForms.TextBox
Public Sub Initialize(ByVal ctrl As MSForms.TextBox)
    Set encapsulated = ctrl
End Sub
Private Sub encapsulated_Change()
Dim TextEmpty As Boolean
Dim cnt As Control
Dim i As Integer

For i = 0 To UserForm1.MultiPage1.Pages.Count - 1
    For Each cnt In UserForm1.MultiPage1.Pages(i).Controls
        If TypeOf cnt Is MSForms.TextBox Then
        'Debug.Print cnt.Name & i & "checked"
            If cnt.Value = "" Then
            TextEmpty = True
            Exit For
            End If
        End If
    Next cnt
    If TextEmpty = True Then
    Exit For
    End If
Next i


If TextEmpty Then
UserForm1.CommandButton2.Enabled = False
Else
UserForm1.CommandButton2.Enabled = True
End If

End Sub

尝试并发现有效

答案 1 :(得分:0)

更简单的方法是单击进行验证:在CommandButton2_Click中,扫描动态创建的文本框,然后继续进行操作或将任何验证错误通知用户。

更复杂的方法是创建一个类,该类将监视TextBox的事件。您将为每个要监视的TextBox创建一个此类的实例,将这些实例保留在例如数组。参见How to add events to Controls created at runtime in Excel with VBA

答案 2 :(得分:0)

您可以遍历工作簿中的每个工作表,并为每个工作表-遍历所有OLEObject。您将检查.Object的类型名称,并在那里进行最终测试。

我将创建一个可以轻松调用以执行此检查并返回布尔值True / False的函数。

Function allTextboxEmpty() As Boolean

    Dim oleObj As OLEObject, ws As Worksheet
    allTextboxEmpty = True

    For Each ws In ThisWorkbook.Worksheets
        For Each oleObj In ws.OLEObjects
            If TypeName(oleObj.Object) = "TextBox" Then
                If oleObj.Object.Value <> vbNullString Then
                    allTextboxEmpty = False
                    Exit Function
                End If
            End If
        Next oleObj
    Next ws

End Function

如果上述函数返回True,则说明您在工作簿中的所有文本框均为空。您可以使用下面的示例中所示的功能:

If allTextboxEmpty Then
    Worksheets("Sheet1").CommandButton2.Enabled = False
Else
    Worksheets("Sheet1").CommandButton2.Enabled = True
End If