ThisWorkbook.Sheets(1).Select (False) Not Working

2020-04-05 08:04发布

I have had a piece of code in operation for over 3 years. Suddenly on July 28th, 2016, it stopped working.

It is very simple and I hope it is an easy solve (or maybe a Microsoft update broke it)

ThisWorkbook.Sheets(1).Select
ThisWorkbook.Sheets(2).Select (False) ' like holding ctrl

This would always selects Sheet #1 AND Sheet #2. Now it seems that the "(False)" doesn't work and it will only select Sheet #1. I have tried this on 5 different computers (all Excel 2013) Please let me know what is going on.

Thanks! -Mike

Edit: This also doesn't work anymore. Like Jordan said in the comments, it just does not execute.

y = 9
ThisWorkbook.Sheets(1).Select

For y = 2 To x

       ThisWorkbook.Sheets(y).Select (False) ' like holding ctrl

Next y

edit2: Since there doesn't seem to be a definitive answer I will ask if somebody can help me with a workaround:

ThisWorkbook.Sheets(Array(1 to x)).ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    FolderName & "\" & QuoteFilename, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, OpenAfterPublish:=False

Obviously this does not work, but it should get my point across.

SOLUTION:

Thanks to Ralph, I took some excerpts and created this:

Private Sub Tester()
x = 5
ReDim SheetstoSelect(1 To x) As String

For y = 1 To x
    SheetstoSelect(y) = ThisWorkbook.Sheets(y).Name
Next y
ThisWorkbook.Sheets(SheetstoSelect).Select

End Sub

This selects the actual Sheet# from 1-5 and allows defining sheets to select by their actual sheet order.

Still don't know the root of the initial issue, but workarounds are just as good.

3条回答
男人必须洒脱
2楼-- · 2020-04-05 08:59

The following lines of code will select all sheets in the workbook the macro is called from:

Option Explicit

Public Sub SelectAllSheetsInThisFile()

Dim x As Long
Dim SheetstoSelect() As String

ReDim SheetstoSelect(1 To ThisWorkbook.Worksheets.Count)

For x = 1 To ThisWorkbook.Worksheets.Count
    SheetstoSelect(x) = ThisWorkbook.Worksheets(x).Name
Next x
ThisWorkbook.Worksheets(SheetstoSelect).Select

End Sub

The following sub will just select the two sheets you asked for in your original post:

Option Explicit

Public Sub SelectYourSheets()

Dim SheetstoSelect(1 To 2) As String
SheetstoSelect(1) = ThisWorkbook.Worksheets(1).Name
SheetstoSelect(2) = ThisWorkbook.Worksheets(2).Name
ThisWorkbook.Worksheets(SheetstoSelect).Select

End Sub

If you prefer to have it all in one line then you can also use split to create an array on the fly like this:

ThisWorkbook.Worksheets(Split("Sheet1/Sheet3", "/")).Select

This line of code will select two sheets with the names Sheet1 and Sheet3. I chose the delimiter / because this character cannot be used in a sheet's name.

Just on a side note: I agree with @BruceWayne. You should try to avoid using select altogether (if possible).

查看更多
叼着烟拽天下
3楼-- · 2020-04-05 09:01

I had the same issue today. Probably delayed because of the update-schedule of my company; likely still the same update. I found your thread and then just before implementing your workaround I found a much simpler one:

ThisWorkbook.Sheets(1).Select
ThisWorkbook.Sheets(2).Select (False) ' like holding ctrl
ThisWorkbook.Sheets(3).Select (False) 

does not work anymore, but

ThisWorkbook.Sheets(1).Select
ThisWorkbook.Sheets(2).Select (False) ' like holding ctrl
ThisWorkbook.Sheets(3).Select (False) 
ThisWorkbook.Sheets(2).Select (False) ' line 2 again; essential sacrifice for the vba-gods.

does.

查看更多
爷、活的狠高调
4楼-- · 2020-04-05 09:07

I had VBA that was working perfectly until the first week of August, then my PDFs only had the first page. I was using a similar method as you - where I'd select many worksheets. I did a work around using an array. My code was within a form, but I'll post here for reference.

Private Sub CommandButton2_Click()

Dim PrintArray() As Variant

'I used a form to select with checkboxes which worksheets to print, so this code would go inside the form linked to a command button

ReDim Preserve PrintArray(1 To 1)
PrintArray(1) = "Sheet 1 Name"
   j = 1

If Sheet2.Value = True Then  'I used a checkbox to select which worksheets to print, but you could use any conditional statement here
    j = j + 1
    ReDim Preserve PrintArray(1 To j)
    PrintArray(j) = "Sheet 2 Name"
End If

If Sheet3.Value = True Then 'I used a checkbox to select which worksheets to print, but you could use any conditional statement here
    j = j + 1
    ReDim Preserve PrintArray(1 To j)
    PrintArray(j) = "Sheet 3 Name"
End If

'You could add as many pages and conditions as you need....

Unload Me 'because I was using a form

Sheets(PrintArray).Select


'Creates the PDF file name
FileNameforSave = "Name of New File" & ".pdf"

'Save file as a PDF
ActiveSheet.ExportAsFixedFormat xlTypePDF, Filename:= _
        FileNameforSave, _
        Quality:=xlQualityStandard, IncludeDocProperties:= _
        True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
查看更多
登录 后发表回答