Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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.

like image 284
mxc1090 Avatar asked Aug 02 '16 15:08

mxc1090


People also ask

What is the difference between ActiveWorkbook and ThisWorkbook?

ThisWorkbook refers to the workbook in which Excel VBA code is being executed. ActiveWorkbook on the other hand refers to the Excel Workbook that current has focus, meaning is the front facing Excel Window. Often Excel VBA Developers mix these two common types of Workbooks in VBA.

What does ThisWorkbook mean in VBA?

VBA ThisWorkbook means the workbook in which we are writing the Excel code. So, for example, if you are working in the workbook named “Sales 2019. xlsx,” we usually refer to the workbook like this. Workbooks(“Sales 2019.xlsx”).Activate. The code will activate the workbook named “Sales 2019.


2 Answers

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).

like image 104
Ralph Avatar answered Nov 15 '22 09:11

Ralph


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
like image 27
Shana Anderson Avatar answered Nov 15 '22 08:11

Shana Anderson