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

后端 未结 3 925
长发绾君心
长发绾君心 2021-01-18 15:28

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

相关标签:
3条回答
  • 2021-01-18 15:42

    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
    
    0 讨论(0)
  • 2021-01-18 15:48

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

    0 讨论(0)
  • 2021-01-18 16:00

    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.

    0 讨论(0)
提交回复
热议问题