我正在使用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
答案 0 :(得分:0)
已在评论中更正,但可以澄清问题所在:
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")
在此处指定要打开的工作簿,并在“默认”工作表上打开。工作表规范用于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")