开发者

Excel VBA 2007: Select all sheets except for specified ones (to perform actions onto)

I'm trying to create a macro that selects most of the sheets in the workbook and then uses ExportAsFixedFormat to print them to PDF.

The pr开发者_开发百科oblem is, while I know the sheets I don't want to include, I don't know exactly which sheets will be in place in at given time, so I can't make a static list of "Select these sheets" (It'd probably be painful to maintain anyway).

Is there some way to, say, "Select all then unselect these specific sheets" or something similar?


How about:

Dim s As Worksheet
DoNotInclude = "Sheet2"
For Each s In ActiveWorkbook.Worksheets
    If InStr(DoNotInclude, s.Name) = 0 Then
        ''Do something
    End If
Next


There isn't. Which doesn't prevent you from doing it yourself:

Public Sub SelectExceptThese(ByVal wb As Workbook, ExceptThese() As String)

  Dim ws() As String
  ReDim ws(1 To wb.Worksheets.Count)

  Dim wt As Worksheet
  Dim i As Long
  For Each wt In wb.Worksheets
    If Not IsInArray(ExceptThese, wt.Name) Then
      i = i + 1
      ws(i) = wt.Name
    End If
  Next

  If i > 0 Then
    ReDim Preserve ws(LBound(ws) To LBound(ws) + i - 1)
    wb.Worksheets(ws).Select
  End If

End Sub

Private Function IsInArray(arr() As String, val As String) As Boolean
  Dim i As Long

  For i = LBound(arr) To UBound(arr)
    If arr(i) = val Then
      IsInArray = True
      Exit Function
    End If
  Next
End Function


This might be a bit too late to answer however might be helpful for others.

If you know what sheets you don't want to Export And If these Sheets are the same all the time you can do the following..

Sheets("sheetnotincluded1").Visible = False
Sheets("sheetnotincluded2").Visible = False

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"FilePath.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True

 Sheets("sheetnotincluded1").Visible = True
 Sheets("sheetnotincluded2").Visible = True

You are simply hiding unwanted sheets, exporting the active workbook and unhiding unwanted sheets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜