我找到并调整了一个宏来创建一个显示工作簿中所有活动工作表的用户窗体。用户可以选择他/她想要打印的所有纸张。单击“确定”按钮将打印所有选定的工作表。我更改了这个宏,所以默认情况下它会选择最常见的纸张(如果纸张的单元格B98中有"是")。
现在我正试图创建一个"选择所有"按钮和"取消选择所有"按钮。单击时,该按钮调用Sub Select_All()。我想我可以告诉Sub Select_All()更改我的活动工作簿中所有复选框的状态。不幸的是它给了我:
运行时错误'''
:对象不支持此属性或方法
创建用户表单的代码:
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
点击全选按钮调用的代码: Sub Select_All()
Dim CB As CheckBox
For Each CB In ActiveWorkbook
CB.Value = xlOn
Next CB
End Sub
我已经搜索过这个网站和其他人,并尝试了很多其他选项,但我无法让它发挥作用。
非常感谢任何帮助。
提前致谢。
答案 0 :(得分:0)
仅供参考,您没有使用用户表单,而是使用了老式的对话框。无论如何,你也需要遍历对话框表:
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