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



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

With Workbooks.Open(fileName:="C:Usersu333161DesktopHGIsGVII-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:Usersu333161DesktopHGIsGVII-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:Usersu333161DesktopHGIsGVII-G600 Stress Report Burndown Master (plus GSNs) 3Q Rev 8-22 update.xlsx", ReadOnly:=True).Worksheets(1).Range("A1:X2500")

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

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

Set columnRange = Range("I1: I2500")

因此,您需要先激活适当的工作表:

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

相关内容

最新更新