我目前正在尝试构建一个按钮单击打印宏,以打印除两张纸(当前共5张纸)以外的所有纸。另外,我还有第三张纸,希望用户定义要打印多少页。当我尝试使它基于某个单元格时,出现错误1004。要查看其余代码是否正常工作,并让该模块具有定义的数量,excel在执行第一张打印后会在我身上崩溃。
Sub Button1_Click()
Dim Wks As Worksheet, xcell As Integer
'The next line is where I get 1004 but when I change it to a fixed number it crashes excel'
xcell = Sheets("Print Page").Range("B12").Value
If xcell < 1 Then
MsgBox ("Please Enter the number of pages needed")
Exit Sub
Else
For Each Wks In ActiveWorkbook.Worksheets
If Wks.Visible = xlSheetVisible Then
If Wks.Name = "Print Page" Then
Else
If Wks.Name = "Specs" Then
Else
If Wks.Name = "Data" Then
Wks.PrintOut From:=1, To:=xcell
Else
Wks.PrintOut
End If
End If
End If
End If
Next Wks
End If
End Sub
可能有一种我不知道的更干净的书写方式。
答案 0 :(得分:0)
不确定这些在Mac上是否可以使用。如果B12小于1,下面将继续提示> 0的值。简化了工作表条件,并引用了宏所在工作簿中的所有对象。
Sub Button1_Click()
Dim Wks As Worksheet, xcell As Long, sInput As String
With ThisWorkbook
With .Worksheets("Print Page").Range("B12")
xcell = CLng(.Value)
If xcell < 1 Then
Do
sInput = InputBox("Please Enter the number of pages needed:", "Pages to print", 1)
If Len(sInput) = 0 Then Exit Sub ' Abort if clicked Cancel/left empty and clicked OK
If IsNumeric(sInput) Then xcell = CLng(sInput)
Loop Until xcell > 0
.Value = xcell
End If
End With
For Each Wks In .Worksheets
If Wks.Visible = xlSheetVisible Then
Select Case Wks.Name
Case "Print Page", "Specs" ' Skip!
Case "Data"
Wks.PrintOut From:=1, To:=xcell
Case Else
Wks.PrintOut
End Select
End If
Next Wks
End With
End Sub