ThisWorkbook.Sheets (1) .Select (False) Doesn't work

I have a piece of code that has been working for more than 3 years. Suddenly, on July 28, 2016, he stopped working.

It is very simple, and I hope it is easy to solve (or perhaps a Microsoft update broke it)

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

This will always select sheet # 1 and sheet # 2. Now it seems that "(False)" is not working, and it will only select sheet # 1. I tried this on 5 different computers (all Excel 2013) Please let me know what happens.

Thanks! -Mike

Edit: This also does not work anymore. As Jordan said in the comments, this is simply not being implemented.

 y = 9 ThisWorkbook.Sheets(1).Select For y = 2 To x ThisWorkbook.Sheets(y).Select (False) ' like holding ctrl Next y 

edit2: Since there seems to be no final answer, I will ask if anyone can help me with the workaround:

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

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

DECISION:

Thanks to Ralph, I made some passages 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 No. 1-5 and allows you to define the sheets for selection according to their actual sheet order.

I still don't know the root of the original problem, but workarounds are just as good.

+6
source share
3 answers

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

 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 simply select two sheets you requested 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 on one line, 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 named Sheet1 and Sheet3 . I chose the delimiter / because this character cannot be used in the sheet name.

Just on the side of the note: I agree with @BruceWayne. You should try to avoid using select at all (if possible).

+3
source

I had a VBA that worked fine until the first week of August, then my PDF files had only the first page. I used the same method as you - where I would select many worksheets. I worked with an array. My code was on the form, but I will send it 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 
+1
source

I had the same problem today. There is probably a delay due to the update schedule of my company; probably the same update. I found your thread, and then, before implementing my workaround, I found a much simpler option:

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

no longer works 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.

0
source

All Articles