动态检查与工作表同名的复选框 -VBA Excel
Dynamically Checking for Checkboxes with the same name as Worksheets -VBA Excel
我在 excel,我有大约 30 个用户窗体复选框,每个复选框都位于名为 "Summary" 的页面上的一个大列中。每一个对应一个作品sheet。我试图遍历这些复选框以查看哪些已被选中,哪些未被选中。我已经将一些代码放在所有工作的循环中 sheets,这是不起作用的位:
Private Sub Run_Click()
Dim SummaryFirstBlankRow As Integer
Dim LastRow As Integer
Dim LastRowH As Integer
Dim rng As Range
Dim SumOfHours As Variant
Dim EndLoop As Boolean
Dim DupeRowNo As Integer
Dim RowNo2 As Integer
Dim ClearContent As Boolean
Dim cbtrue As Boolean
Dim ws As Worksheet
cbtrue = False
For Each ws In ActiveWorkbook.Worksheets
LastRowH = ws.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0).Row
'If ActiveWorkbook.Sheets("Summary").CheckBoxes(ws.name).Value = False Then ' looks to see if ws.names' checkbox is checked or not
' ClearContent = True 'if not checked, sets clear content to true
'End If
For Each CheckBox In Sheets("Summary").CheckBoxes
If CheckBox.name = ws.name Then
If CheckBox.Value = False Then
ClearContent = True
End If
End If
Next CheckBox
If ClearContent = True Then
For c = 1 To LastRowH 'if not checked, looks on current worksheet (within loop) and if any "Y" or "y" vals, clears them
If ws.Range("H" & c).Value = "Y" Or ws.Range("H" & c).Value = "y" Then
ws.Range("H" & c).ClearContents
End If
Next
End If
...
cbtrue 只是一个变量,用于查看复选框是否存在,因此如果存在,它将转到 if 语句,此时它将确定该复选框是否被勾选,这取决于它保存ClearContent 变量(我稍后在代码中使用)。
问题是涉及到 'Shapes("ws.Name")' 时,ws.name 只是每个循环中作品的名称 sheet。所以在循环的第一轮,它将是 "Summary"... 但是,我认为它在物理上搜索显然不存在的 sheet "ws.name" 。我试过从引号中删除它,也尝试过其他各种方法,例如 'Checkboxes("ws.Name")' 但似乎它们都有同样的问题。
我发帖是想看看是否有人可以提供另一种方法,或者告诉我哪里出错了,因为我认为我没有完全理解语法。
感谢任何帮助。提前致谢:)
更新
我在@Xabier 的帮助下更改了代码(我添加了一个额外的 if 语句以确保 CheckBox 与作品同名sheet。我现在没有收到任何错误,但是当我 运行 它时,它并没有清除我请求的任何单元格的内容。我似乎无法发现它为什么这样做,如果有人能发现它并告诉我,那就是太好了。谢谢 :)
下面的代码将向您展示如何遍历工作表上的 ActiveX 复选框并验证它们是否被选中:
Sub Test()
Dim obj As OLEObject
For Each obj In Sheets("Summary").OLEObjects
'loop through ActiveX Checkboxes on Worksheet "Summary"
If obj.progID = "Forms.CheckBox.1" Then
'is it a Checkbox?
If obj.Object = False Then
'is the checkbox unchecked
ClearContent = True
End If
End If
Next obj
End Sub
更新:
对于表单控件复选框,将执行以下操作:
Sub Test2()
Dim cb As CheckBox
For Each cb In Sheets("Summary").CheckBoxes
If cb.Value = False Then
ClearContent = True
End If
Next cb
End Sub
您的代码肯定会将 "ws.name" 视为文字字符串,因为它包含在双引号中。单击复选框后,您可以直接将 ClearContent 变量设置为 true 或 false。这样你就不用去测试它的值了。
您的代码有两个问题:
方面 1:名称还是文本?
我认为这里可能存在问题的是理解复选框的名称 Excel:
您在复选框旁边看到的文字不是复选框的实际名称。您可以通过单击鼠标右键选择复选框并在左上角框中输入所需的名称来为复选框命名。
使用此示例代码查看您的复选框实际使用的名称和显示文本:
For Each CheckBox In Sheets("Summary").CheckBoxes
MsgBox "Name: " & CheckBox.Name & " Text: " & CheckBox.Text
Next CheckBox
它会产生这样的东西:
如果我是正确的,要解决您的问题,您必须为复选框提供正确的名称(工作表的名称)或者不比较 Name
,而是使用 Text
属性 复选框。
方面 2:值不正确/错误
复选框的值不是 True
或 False
,但复选框的值可以是 xlOn
或 xlOff
。所以,不要测试 True
或 False
,而是使用正确的常量。
工作代码使用显示的文本而不是名称并使用正确的值常量
Dim ws As Worksheet
Dim ClearContent As Boolean
Dim CheckBox As CheckBox
For Each ws In ActiveWorkbook.Worksheets
' Reset ClearContent for each Sheet
ClearContent = False
For Each CheckBox In Sheets("Summary").CheckBoxes
' Depending on your requests, compare either with Name or with Text
If CheckBox.Text = ws.Name Then
' Use xlOff constant instead of False
If CheckBox.Value = xlOff Then
ClearContent = True
End If
' We can exit the foreach loop when we found the correct checkbox
Exit For
End If
Next CheckBox
If ClearContent Then
MsgBox "Going to clear " & ws.Name
End If
Next ws
我在 excel,我有大约 30 个用户窗体复选框,每个复选框都位于名为 "Summary" 的页面上的一个大列中。每一个对应一个作品sheet。我试图遍历这些复选框以查看哪些已被选中,哪些未被选中。我已经将一些代码放在所有工作的循环中 sheets,这是不起作用的位:
Private Sub Run_Click()
Dim SummaryFirstBlankRow As Integer
Dim LastRow As Integer
Dim LastRowH As Integer
Dim rng As Range
Dim SumOfHours As Variant
Dim EndLoop As Boolean
Dim DupeRowNo As Integer
Dim RowNo2 As Integer
Dim ClearContent As Boolean
Dim cbtrue As Boolean
Dim ws As Worksheet
cbtrue = False
For Each ws In ActiveWorkbook.Worksheets
LastRowH = ws.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0).Row
'If ActiveWorkbook.Sheets("Summary").CheckBoxes(ws.name).Value = False Then ' looks to see if ws.names' checkbox is checked or not
' ClearContent = True 'if not checked, sets clear content to true
'End If
For Each CheckBox In Sheets("Summary").CheckBoxes
If CheckBox.name = ws.name Then
If CheckBox.Value = False Then
ClearContent = True
End If
End If
Next CheckBox
If ClearContent = True Then
For c = 1 To LastRowH 'if not checked, looks on current worksheet (within loop) and if any "Y" or "y" vals, clears them
If ws.Range("H" & c).Value = "Y" Or ws.Range("H" & c).Value = "y" Then
ws.Range("H" & c).ClearContents
End If
Next
End If
...
cbtrue 只是一个变量,用于查看复选框是否存在,因此如果存在,它将转到 if 语句,此时它将确定该复选框是否被勾选,这取决于它保存ClearContent 变量(我稍后在代码中使用)。
问题是涉及到 'Shapes("ws.Name")' 时,ws.name 只是每个循环中作品的名称 sheet。所以在循环的第一轮,它将是 "Summary"... 但是,我认为它在物理上搜索显然不存在的 sheet "ws.name" 。我试过从引号中删除它,也尝试过其他各种方法,例如 'Checkboxes("ws.Name")' 但似乎它们都有同样的问题。
我发帖是想看看是否有人可以提供另一种方法,或者告诉我哪里出错了,因为我认为我没有完全理解语法。
感谢任何帮助。提前致谢:)
更新
我在@Xabier 的帮助下更改了代码(我添加了一个额外的 if 语句以确保 CheckBox 与作品同名sheet。我现在没有收到任何错误,但是当我 运行 它时,它并没有清除我请求的任何单元格的内容。我似乎无法发现它为什么这样做,如果有人能发现它并告诉我,那就是太好了。谢谢 :)
下面的代码将向您展示如何遍历工作表上的 ActiveX 复选框并验证它们是否被选中:
Sub Test()
Dim obj As OLEObject
For Each obj In Sheets("Summary").OLEObjects
'loop through ActiveX Checkboxes on Worksheet "Summary"
If obj.progID = "Forms.CheckBox.1" Then
'is it a Checkbox?
If obj.Object = False Then
'is the checkbox unchecked
ClearContent = True
End If
End If
Next obj
End Sub
更新: 对于表单控件复选框,将执行以下操作:
Sub Test2()
Dim cb As CheckBox
For Each cb In Sheets("Summary").CheckBoxes
If cb.Value = False Then
ClearContent = True
End If
Next cb
End Sub
您的代码肯定会将 "ws.name" 视为文字字符串,因为它包含在双引号中。单击复选框后,您可以直接将 ClearContent 变量设置为 true 或 false。这样你就不用去测试它的值了。
您的代码有两个问题:
方面 1:名称还是文本?
我认为这里可能存在问题的是理解复选框的名称 Excel:
您在复选框旁边看到的文字不是复选框的实际名称。您可以通过单击鼠标右键选择复选框并在左上角框中输入所需的名称来为复选框命名。
使用此示例代码查看您的复选框实际使用的名称和显示文本:
For Each CheckBox In Sheets("Summary").CheckBoxes
MsgBox "Name: " & CheckBox.Name & " Text: " & CheckBox.Text
Next CheckBox
它会产生这样的东西:
如果我是正确的,要解决您的问题,您必须为复选框提供正确的名称(工作表的名称)或者不比较 Name
,而是使用 Text
属性 复选框。
方面 2:值不正确/错误
复选框的值不是 True
或 False
,但复选框的值可以是 xlOn
或 xlOff
。所以,不要测试 True
或 False
,而是使用正确的常量。
工作代码使用显示的文本而不是名称并使用正确的值常量
Dim ws As Worksheet
Dim ClearContent As Boolean
Dim CheckBox As CheckBox
For Each ws In ActiveWorkbook.Worksheets
' Reset ClearContent for each Sheet
ClearContent = False
For Each CheckBox In Sheets("Summary").CheckBoxes
' Depending on your requests, compare either with Name or with Text
If CheckBox.Text = ws.Name Then
' Use xlOff constant instead of False
If CheckBox.Value = xlOff Then
ClearContent = True
End If
' We can exit the foreach loop when we found the correct checkbox
Exit For
End If
Next CheckBox
If ClearContent Then
MsgBox "Going to clear " & ws.Name
End If
Next ws