Excel 按名称打印工作表

发布于 2024-07-17 11:43:19 字数 189 浏览 16 评论 0原文

我有一个 Excel 2007 电子表格,我想编写一个 VBA 过程来按名称打印特定的工作表。 我该怎么做呢?

例如, 我想打印 "FirstSheet"、"ThirdSheet","FourthSheet" 但不打印 "SecondSheet"。

I've got an Excel 2007 Spreadsheet and I'd like to write a VBA procedure to print particular worksheets by name. How do I do this?

For example,
I'd like to print "FirstSheet","ThirdSheet", and "FourthSheet" but not "SecondSheet".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放我走吧 2024-07-24 11:43:19

如果您知道工作表名称,只需调用 PrintOut 函数,如下所示:

Sheets("Name").PrintOut

对于少量工作表,通过这种方式更容易!

If you know the sheet name simply call the PrintOut function like this:

Sheets("Name").PrintOut

For small amounts of sheets it is quite more easy by this way!

清风疏影 2024-07-24 11:43:19

类似以下内容应该有效:

Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
        If (sh.Name = "Sheet1") Then
           sh.PrintOut
        End If
Next sh

Something similar to the following should work:

Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
        If (sh.Name = "Sheet1") Then
           sh.PrintOut
        End If
Next sh
吹梦到西洲 2024-07-24 11:43:19
Public Sub PrintByName(Names As Variant)

  Dim s As Worksheet
  Dim i As Integer

  If IsArray(Names) Then
    For Each s In ActiveWorkbook.Worksheets
      For i = 0 To UBound(Names)
        If StrComp(s.Name, Names(i), vbTextCompare) = 0 Then
          s.PrintOut
        End If
      Next i
    Next s
  End If

End Sub

调用方式:

PrintByName Array("FirstSheet", "ThirdSheet", "FourthSheet")

就运行时性能而言,嵌套循环不是最佳的。 由于 Excel 工作簿可以包含的工作表数量有限,我认为这是可以忽略不计的。 但是,使用 Collection 来包含所需的工作表名称而不是 Array 会更好。

Public Sub PrintByName(Names As Variant)

  Dim s As Worksheet
  Dim i As Integer

  If IsArray(Names) Then
    For Each s In ActiveWorkbook.Worksheets
      For i = 0 To UBound(Names)
        If StrComp(s.Name, Names(i), vbTextCompare) = 0 Then
          s.PrintOut
        End If
      Next i
    Next s
  End If

End Sub

Call like:

PrintByName Array("FirstSheet", "ThirdSheet", "FourthSheet")

The nested loop is not optimal, in regards to runtime performance. With the limited number of sheets an Excel workbook can contain, I think this is negligible. However, using a Collection to contain the desired sheet names instead of an Array would be better.

找个人就嫁了吧 2024-07-24 11:43:19

不需要循环来执行此操作,一行代码就足够了:

Sheets(Array("FirstSheet", "ThirdSheet", "FourthSheet")).PrintOut Copies:=1

Don't need to loop to do this, one line of code will suffice:

Sheets(Array("FirstSheet", "ThirdSheet", "FourthSheet")).PrintOut Copies:=1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文