I am working on a current project with Excel VBA, and it involves opening a pre-existing workbook with content already on it. I'm not doing anything particularly complex, only copying content from the other workbook and pasting it in the workbook that I already had open. At any rate, when I open the workbook with VBA, it opens it on the second Worksheet, regardless of what I change my code to, which is confusing because there was a point that the code I have now worked just fine, but now it seems to be broken for whatever reason; here's the format of it as it was before it broke:
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
Any help would be greatly appreciated; thanks so much and have a great afternoon.
For those of you that wanted the content in between the With statements, this is 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")
'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
Corrected in comments, but to clarify what's wrong:
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")
Here you specify a Workbook to open and it opens on "default" worksheet. Worksheet specification is used for With
statement, not for Workbook.Open
.
First you work with your With
, but at some point you begin to work on active worksheet:
Set columnRange = Range("I1: I2500")
So you need to activate proper worksheet first:
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")