简体   繁体   English

ThisWorkbook.Sheets(1).Select(False)不起作用

[英]ThisWorkbook.Sheets(1).Select (False) Not Working

I have had a piece of code in operation for over 3 years. 我已经运行了一段代码超过3年。 Suddenly on July 28th, 2016, it stopped working. 2016年7月28日,它突然停止工作。

It is very simple and I hope it is an easy solve (or maybe a Microsoft update broke it) 非常简单,我希望这是一个简单的解决方法(或者可能是Microsoft更新将其解决)

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

This would always selects Sheet #1 AND Sheet #2. 这将始终选择工作表1和工作表2。 Now it seems that the "(False)" doesn't work and it will only select Sheet #1. 现在看来“(False)”不起作用,它只会选择工作表#1。 I have tried this on 5 different computers (all Excel 2013) Please let me know what is going on. 我已经在5台不同的计算机上(全部都是Excel 2013)进行了尝试,请告诉我发生了什么。

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: edit2:由于似乎没有一个明确的答案,我会问是否有人可以帮助我解决问题:

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: 感谢Ralph,我摘录了一些内容并创建了它:

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. 这将从1-5中选择实际的工作表编号,并允许定义工作表以其实际工作表顺序进行选择。

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: 以下代码行将select调用该宏的工作簿中的所有工作表:

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: 以下子类别将仅select您在原始帖子中要求的两张纸:

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: 如果您希望将所有内容都放在一行中,那么还可以使用split即时创建一个数组,如下所示:

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

This line of code will select two sheets with the names Sheet1 and Sheet3 . 此行代码将select两个名为Sheet1Sheet3 I chose the delimiter / because this character cannot be used in a sheet's name. 我选择定界符/是因为该字符不能用于工作表的名称。

Just on a side note: I agree with @BruceWayne. 附带说明一下:我同意@BruceWayne。 You should try to avoid using select altogether (if possible). 您应该尽量避免完全使用select (如果可能)。

I had VBA that was working perfectly until the first week of August, then my PDFs only had the first page. 我的VBA一直运行到八月的第一周为止,然后我的PDF只有第一页。 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

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. 做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM