【发布时间】:2019-01-30 06:02:42
【问题描述】:
我正在使用 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 Open (...).Sheets(1).Range("A1:X2500")? -
我也想提一下cybernetic.nomad...如果我自己打开文件,它也会在第二个工作表上打开。对此感到抱歉。
-
我确实尝试过 Pm Duda 和 BruceWayne 的做法,并且在它坏掉之前也能正常工作。我在它停止工作后将其包括在内,并且打开哪个并没有什么区别。
标签: excel vba excel-formula excel-2010 excel-2007