我有 VBA 代码,可在工作表 Line Item Summary
之间添加标记为“成本核算表 (2)”、“成本核算表 (3)”等的新工作表(表 7)和 Comparison Job
(表 17)。我不知道要添加多少张,这是由用户决定的。
如何打印添加在两者之间的工作表?本质上,我想要 Line Item Summary
加上任何要打印的“成本核算表” 不是 Comparison Job
(仅限于此)。
**编辑:下面的代码用于添加新工作表
Sub NewSheet()
Worksheets("Costing Sheet").Copy After:=Worksheets("Line Item Summary")
Worksheets(Worksheets("Costing Sheet").Index + 1).Activate
'Active Tab
ActiveSheet.Tab.ColorIndex = xlColorIndexNone
End Sub
最佳答案
这是另一个使用 LIKE
的版本和 Wildcard (*)
.这将只检查 的工作表。需要要打印。
Sub Sample()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Line Item Summary" Or ws.Name Like "Costing Sheet*" Then
ws.PrintOut
End If
Next ws
End Sub
Will keep that in mind! Can this code be altered to perform the opposite, as suggested by the previous code? Users might change the names of these "Costing Sheets (*)" but will still be in between Line Item Summary and Comparison Job – icalderon 31 mins ago
这是你正在尝试的吗?我照顾了几个支票 .您可能需要对其进行编辑以使其更加健壮。
Option Explicit
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim wsStartIndex As Long, wsEndIndex As Long
Dim i As Long
Dim wsName As String
'~~> Loop through the sheet and get the positions of the sheets
For i = 1 To ThisWorkbook.Worksheets.Count
wsName = ThisWorkbook.Sheets(i).Name
If wsName = "Line Item Summary" Then wsStartIndex = ThisWorkbook.Sheets(i).Index
If wsName = "Comparison Job" Then wsEndIndex = ThisWorkbook.Sheets(i).Index
If wsStartIndex <> 0 And wsEndIndex <> 0 Then Exit For
Next i
'~~> Do necessary checks
If wsStartIndex = 0 Then
MsgBox "Line Item Summary Sheet not found"
ElseIf wsEndIndex = 0 Then
MsgBox "Comparison Job Sheet not found"
ElseIf wsEndIndex < wsStartIndex Then
MsgBox "Comparison Job is before Line Item Summary. Sheets cannot be printed"
'~~> If everything is ok then print
ElseIf wsStartIndex <> 0 And wsEndIndex <> 0 Then
For i = wsStartIndex To (wsEndIndex - 1)
If ThisWorkbook.Sheets(i).Name <> "Costing Sheet" Then
ThisWorkbook.Sheets(i).pritnout
End If
Next i
End If
End Sub
关于excel - 如何打印一系列纸张?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59843289/