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.
The following lines of code will
select
all sheets in the workbook the macro is called from:The following sub will just
select
the two sheets you asked for in your original post: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:This line of code will
select
two sheets with the namesSheet1
andSheet3
. 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).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:
does not work anymore, but
does.
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.