ThisWorkbook.Sheets(1).Select(False)Not Working [英] ThisWorkbook.Sheets(1).Select (False) Not Working
问题描述
这是很简单的,我希望这是一个容易的解决(或者也许是一个微软更新破坏它)
ThisWorkbook.Sheets(1)。选择
ThisWorkbook.Sheets(2).Select(False )'像持有ctrl
这将始终选择Sheet#1和Sheet#2。现在看来,(False)不起作用,只能选择Sheet#1。我已经在5台不同的电脑上试过了(所有Excel 2013)请让我知道发生了什么。
谢谢!
-Mike
编辑:
这也不工作了。像约旦在评论中说的,它只是不执行。
y = 9
ThisWorkbook.Sheets(1) 。选择
对于y = 2到x
ThisWorkbook.Sheets(y).Select(False)'like holding ctrl
Next y
edit2:
由于似乎没有一个明确的答案,我会问是否有人可以帮助我解决办法:
ThisWorkbook.Sheets(Array(1 to x))。ExportAsFixedFormat Type:= xlTypePDF,Filename := _
FolderName& \& QuoteFilename,Quality:= xlQualityStandard,IncludeDocProperties:= True,_
IgnorePrintAreas:= False,OpenAfterPublish:= False
显然这不起作用,但应该得到我的观点。
解决方案:
私人子测试员()
x = 5
ReDim SheetstoSelect(1 To x)As String
对于y = 1至x
SheetstoSelect(y)= ThisWorkbook.Sheets(y).Name
Next y
ThisWorkbook.Sheets(SheetstoSelect)。选择
End Sub
从1-5选择实际的Sheet#,并允许按照实际的表单顺序定义工作表。
仍然不知道初始问题的根源,但解决方法
以下代码行将选择
工作簿中的所有工作表都从宏调用:
Option Explicit
Public Sub SelectAllSheetsInThisFile()
Dim x As Long
Dim SheetstoSelect()As String
ReDim SheetstoSelect(1 To ThisWorkbook.Worksheets.Count)
对于x = 1 To ThisWorkbook.Worksheets.Count
SheetstoSelect (x)= ThisWorkbook.Worksheets(x).Name
下一个x
ThisWorkbook.Worksheets(SheetstoSelect)。选择
End Sub
以下子将只是选择
您在原始帖子中要求的两张表: / p>
Option Explicit
Public Sub SelectYourSheets()
Dim SheetstoSelect(1到2)As String
SheetstoSelect(1)= ThisWorkbook.Worksheets(1).Name
SheetstoSelect(2)= ThisWorkbook.Worksheets(2).Name
ThisWorkbook.Worksheets(SheetstoSelect)。选择
End Sub
如果您希望将它全部放在一行那么你也可以使用 split
创建一个ar ray如下:
ThisWorkbook.Worksheets(Split(Sheet1 / Sheet3,/\")).Select
这行代码将选择
两张名称为 Sheet1
和 Sheet3
。我选择了分隔符 /
,因为这个字符不能用于工作表的名称。
旁注:我同意@BruceWayne。你应该尽量避免使用选择
(如果可能的话)。
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:
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).
这篇关于ThisWorkbook.Sheets(1).Select(False)Not Working的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!