Excel 工作簿仅在第二个工作表上打开

Excel Workbook Opens On Only The Second Worksheet

我正在使用 Excel VBA 进行当前项目,它涉及打开一个已有内容的现有工作簿。我没有做任何特别复杂的事情,只是从另一个工作簿中复制内容并将其粘贴到我已经打开的工作簿中。无论如何,当我用 VBA 打开工作簿时,它会在第二个工作表上打开它,而不管我将代码更改为什么,这很令人困惑,因为有一点我现在的代码工作得很好,但现在似乎不知何故坏了;这是它损坏之前的格式:

With Workbooks.Open(fileName:="C:\Users\u333161\Desktop\HGIs\GVII-G600 Stress Report Burndown Master  (plus GSNs) 3Q Rev 8-22 update.xlsx", ReadOnly:=True).Range("A1:X2500") 
    'All the content that the script contains is here'
End With

如有任何帮助,我们将不胜感激;非常感谢,祝您下午愉快。

对于那些想要 With 语句之间的内容的人来说,就是这样:

With Workbooks.Open(fileName:="C:\Users\u333161\Desktop\HGIs\GVII-G600 Stress Report Burndown Master  (plus GSNs) 3Q Rev 8-22 update.xlsx", ReadOnly:=True).Worksheets(1).Range("A1:X2500")
'Opens the G600 Burndown from the original location ^
.AutoFilter                                                                         'Clears any filters on the document
.AutoFilter 20, "y"                                                                 'Sets "Cert Doc" to filter for "y"
.AutoFilter 13, ""                                                                  'Sets "Release Date" to (blanks)
.Rows.Sort Key1:=Columns("I"), Order1:=xlAscending                                  'Sets "3Q Replan" to sort "Oldest To Newest"
Dim columnRange As Range, cell As Range, comparingDate As Range                     'Declares variables
Set columnRange = Range("I1: I2500")                                                'Sets a variable equal to a range
For Each cell In columnRange                                                        'Loop to go through all the rows in the "I" column
  If CDate(cell.Value) > Now() Then                                                 'Compares each date in the "I" column to today
    Range("A1:X" & cell.Offset(-1, 0).Row).Copy                                     'Copies the entire range above the current if the date for the current row happens after today
    ActiveWorkbook.Close
    Exit For                                                                        'Terminates the for loop because there is no reason to keep looping
  End If                                                                            'End of the if statement
Next cell                                                                           'Goes to the next cell
End With                                                                              'We're done with this sheet, so we can end it

已在评论中更正,但要澄清错误所在:

With Workbooks.Open(fileName:="C:\Users\u333161\Desktop\HGIs\GVII-G600 Stress Report Burndown Master (plus GSNs) 3Q Rev 8-22 update.xlsx", ReadOnly:=True).Worksheets(1).Range("A1:X2500")

在这里您指定要打开的工作簿,它在 "default" 工作表上打开。工作表规范用于 With 语句,不适用于 Workbook.Open.

首先您使用 With,但在某个时候您开始处理活动工作表:

Set columnRange = Range("I1: I2500")

所以您需要先激活正确的工作表:

Workbooks.Open(fileName:="C:\Users\u333161\Desktop\HGIs\GVII-G600 Stress Report Burndown Master (plus GSNs) 3Q Rev 8-22 update.xlsx", ReadOnly:=True) 
Worksheets(1).Activate 
With Range("A1:X2500")