根据此讨论,我尝试重置Excel 2007 UsedRange: Excel resetting "UsedRange"
但是,我遇到了一个我不明白的问题。我收到错误"无效使用财产"在编译时,如果我从Worksheet对象变量重置UsedRange,但不通过Worksheets集合,如下所示:
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set wb = Excel.Workbooks.Add
Set ws = wb.Worksheets(1)
'Why does this not work?
ws.UsedRange
'And why does this instead?
wb.Worksheets(ws.Name).UsedRange
'Note: Absolute object variables are in use because this code will be used in
'an Access database to start & automate Excel. So the "Active_____." objects
'are not used, such as ActiveBook, ActiveSheet, etc.
在我看来,无论哪种方式都应该起作用,但是就像UsedRange背后的代码中缺少的东西一样,它告诉编译器它除了属性之外还有一个命令。
答案 0 :(得分:0)
' Why does this not work?
ws.UsedRange
因为它没有"做"任何东西。它只是一个范围。
你可以像任何其他范围一样操纵它,例如清除它:
ws.UsedRange.Clear