我正在尝试为类调试以下代码。对于我的生活,我似乎无法看到每个陈述的错误。
Public Sub PrintWorksheets1()
'declare variables and assign address to object variable
Dim intPrint As Integer, wkbHours As Workbook, shtCurrent As Worksheets
Set wkbHours = Application.Workbooks("T9-EX-E9D.xls")
'ask user if he or she wants to print the worksheet
For Each shtCurrent In wkbHours
intPrint = _
MsgBox(prompt:="Print " & shtCurrent.Name & "?", Buttons:=vbYesNo + vbExclamation)
If intPrint = vbYes Then 'if user wants to print
shtCurrent.PrintPreview
End If
Next shtCurrent
End Sub
我尝试将shtCurrent设置为活动工作表和工作表。非常感谢任何帮助!
答案 0 :(得分:0)
更新的代码:您几乎就在那里,但您需要遍历工作簿工作表...
Public Sub PrintWorksheets1()
'declare variables and assign address to object variable
Dim intPrint As Integer, shtCurrent As Worksheet
Dim wkbHours As Workbook: Set wkbHours = Workbooks("T9-EX-E9D.xls")
'ask user if he or she wants to print the worksheet
For Each shtCurrent In wkbHours.Worksheets
intPrint = MsgBox(prompt:="Print " & shtCurrent.Name & "?", Buttons:=vbYesNo + vbExclamation)
'if user wants to print
If intPrint = vbYes Then shtCurrent.PrintPreview
Next shtCurrent
End Sub