Select 用户表单上的所有复选框,使用一个按钮
Select all checkboxes on a userform, using a button
我找到并调整了一个宏来创建一个用户窗体,显示我工作簿中的所有活动工作sheet。用户可以select他/她想要打印的所有sheet。单击“确定”按钮将打印所有 selected sheet。我更改了此宏,因此默认情况下它将 select 最常打印的 sheet(如果 sheet 的单元格 B98 中有 "yes")。
现在我正在尝试创建一个 "select all" 按钮和一个 "unselect all" 按钮。单击时,按钮调用 Sub Select_All ()。我以为我可以告诉 Sub Select_All() 更改活动工作簿中所有复选框的状态。不幸的是它给了我:
运行-时间错误'438':
对象不支持此 属性 或方法
创建用户表单的代码:
Sub PrintSelectedSheets()
Dim I As Integer
Dim TopPos As Integer
Dim SheetCount As Integer
Dim PrintDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim CB As CheckBox
Application.ScreenUpdating = False
'Check for protected workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox "Workbook is protected.", vbCritical
Exit Sub
End If
'Add a temporary dialog sheet
Sheets("Cover").Select
Set CurrentSheet = ActiveSheet
Set PrintDlg = ActiveWorkbook.DialogSheets.Add
SheetCount = 0
'Add a select all button
ActiveSheet.Buttons.Add(273, 95, 52.5, 16).Select
Selection.Characters.Text = "Select All"
Selection.OnAction = "Select_All"
'Add a unselect all button
ActiveSheet.Buttons.Add(273, 116, 52.5, 16).Select
Selection.Characters.Text = "Unselect All"
Selection.OnAction = "Unselect_All"
'Add the checkboxes
TopPos = 40
For I = 1 To ActiveWorkbook.Worksheets.Count
Set CurrentSheet = ActiveWorkbook.Worksheets(I)
'Skip empty sheets and hidden sheets
If Application.CountA(CurrentSheet.Cells) <> 0 And _
CurrentSheet.Visible Then
SheetCount = SheetCount + 1
'Change status for all checkboxes to on if B98 = yes
If CurrentSheet.Range("B98") = "yes" Then
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(SheetCount).Value = xlOn
PrintDlg.CheckBoxes(SheetCount).Text = CurrentSheet.Name
TopPos = TopPos + 13
Else
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(SheetCount).Value = xlOff
PrintDlg.CheckBoxes(SheetCount).Text = CurrentSheet.Name
TopPos = TopPos + 13
End If
End If
Next I
'Move the OK and Cancel buttons
PrintDlg.Buttons.Left = 240
'Set dialog height, width, and caption
With PrintDlg.DialogFrame
.Height = Application.Max _
(68, PrintDlg.DialogFrame.Top + TopPos - 34)
.Width = 230
.Caption = "Select sheets to print"
End With
'Change focus to the 1st option button
PrintDlg.Buttons("Button 4").BringToFront
PrintDlg.Buttons("Button 5").BringToFront
'Display the dialog box
Sheets("Cover").Activate
If SheetCount <> 0 Then
If PrintDlg.Show Then
For Each CB In PrintDlg.CheckBoxes
If CB.Value = xlOn Then
Worksheets(CB.Caption).Select Replace:=False
End If
Next CB
ActiveWindow.SelectedSheets.PrintOut copies:=1
ActiveSheet.Select
End If
Else
MsgBox "All worksheets are empty."
End If
'Delete temporary dialog sheet (without a warning)
Application.DisplayAlerts = False
PrintDlg.Delete
'Reactivate original sheet
Sheets("Cover").Activate
Application.ScreenUpdating = True
End Sub
以及通过单击 Select 全部按钮调用的代码:
子 Select_All()
Dim CB As CheckBox
For Each CB In ActiveWorkbook
CB.Value = xlOn
Next CB
End Sub
我已经搜索了这个网站和其他网站并尝试了很多其他选项,但我就是无法让它工作。
非常感谢任何帮助。
提前致谢。
仅供参考,您没有使用用户窗体,您使用的是老式对话框 sheet。无论如何,你也需要循环遍历对话框 sheets:
Dim CB As CheckBox
Dim ds as DialogSheet
For Each ds In ActiveWorkbook.DialogSheets
For Each CB in ds.Checkboxes
CB.Value = xlOn
Next CB
next ds
我找到并调整了一个宏来创建一个用户窗体,显示我工作簿中的所有活动工作sheet。用户可以select他/她想要打印的所有sheet。单击“确定”按钮将打印所有 selected sheet。我更改了此宏,因此默认情况下它将 select 最常打印的 sheet(如果 sheet 的单元格 B98 中有 "yes")。
现在我正在尝试创建一个 "select all" 按钮和一个 "unselect all" 按钮。单击时,按钮调用 Sub Select_All ()。我以为我可以告诉 Sub Select_All() 更改活动工作簿中所有复选框的状态。不幸的是它给了我:
运行-时间错误'438':
对象不支持此 属性 或方法
创建用户表单的代码:
Sub PrintSelectedSheets()
Dim I As Integer
Dim TopPos As Integer
Dim SheetCount As Integer
Dim PrintDlg As DialogSheet
Dim CurrentSheet As Worksheet
Dim CB As CheckBox
Application.ScreenUpdating = False
'Check for protected workbook
If ActiveWorkbook.ProtectStructure Then
MsgBox "Workbook is protected.", vbCritical
Exit Sub
End If
'Add a temporary dialog sheet
Sheets("Cover").Select
Set CurrentSheet = ActiveSheet
Set PrintDlg = ActiveWorkbook.DialogSheets.Add
SheetCount = 0
'Add a select all button
ActiveSheet.Buttons.Add(273, 95, 52.5, 16).Select
Selection.Characters.Text = "Select All"
Selection.OnAction = "Select_All"
'Add a unselect all button
ActiveSheet.Buttons.Add(273, 116, 52.5, 16).Select
Selection.Characters.Text = "Unselect All"
Selection.OnAction = "Unselect_All"
'Add the checkboxes
TopPos = 40
For I = 1 To ActiveWorkbook.Worksheets.Count
Set CurrentSheet = ActiveWorkbook.Worksheets(I)
'Skip empty sheets and hidden sheets
If Application.CountA(CurrentSheet.Cells) <> 0 And _
CurrentSheet.Visible Then
SheetCount = SheetCount + 1
'Change status for all checkboxes to on if B98 = yes
If CurrentSheet.Range("B98") = "yes" Then
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(SheetCount).Value = xlOn
PrintDlg.CheckBoxes(SheetCount).Text = CurrentSheet.Name
TopPos = TopPos + 13
Else
PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
PrintDlg.CheckBoxes(SheetCount).Value = xlOff
PrintDlg.CheckBoxes(SheetCount).Text = CurrentSheet.Name
TopPos = TopPos + 13
End If
End If
Next I
'Move the OK and Cancel buttons
PrintDlg.Buttons.Left = 240
'Set dialog height, width, and caption
With PrintDlg.DialogFrame
.Height = Application.Max _
(68, PrintDlg.DialogFrame.Top + TopPos - 34)
.Width = 230
.Caption = "Select sheets to print"
End With
'Change focus to the 1st option button
PrintDlg.Buttons("Button 4").BringToFront
PrintDlg.Buttons("Button 5").BringToFront
'Display the dialog box
Sheets("Cover").Activate
If SheetCount <> 0 Then
If PrintDlg.Show Then
For Each CB In PrintDlg.CheckBoxes
If CB.Value = xlOn Then
Worksheets(CB.Caption).Select Replace:=False
End If
Next CB
ActiveWindow.SelectedSheets.PrintOut copies:=1
ActiveSheet.Select
End If
Else
MsgBox "All worksheets are empty."
End If
'Delete temporary dialog sheet (without a warning)
Application.DisplayAlerts = False
PrintDlg.Delete
'Reactivate original sheet
Sheets("Cover").Activate
Application.ScreenUpdating = True
End Sub
以及通过单击 Select 全部按钮调用的代码: 子 Select_All()
Dim CB As CheckBox
For Each CB In ActiveWorkbook
CB.Value = xlOn
Next CB
End Sub
我已经搜索了这个网站和其他网站并尝试了很多其他选项,但我就是无法让它工作。
非常感谢任何帮助。
提前致谢。
仅供参考,您没有使用用户窗体,您使用的是老式对话框 sheet。无论如何,你也需要循环遍历对话框 sheets:
Dim CB As CheckBox
Dim ds as DialogSheet
For Each ds In ActiveWorkbook.DialogSheets
For Each CB in ds.Checkboxes
CB.Value = xlOn
Next CB
next ds