2014-01-10 70 views
0

我試圖循環遍歷電子表格中的所有工作表,並將單元格A4的內容清除到工作表的末尾。Excel應用程序定義或對象定義的錯誤

如果電子表格只包含1張工作表,我可以使用它。 如果它包含超過1片,它試圖清除表內容時失敗2.

在錯誤的線路.Range("A4", Range("A4").SpecialCells(xlLastCell)).ClearContents

當它擊中那行,我得到的錯誤「Excel應用程序定義或對象定義的錯誤'

你能看到我在做什麼錯嗎?

Set book = Workbooks.Open("folder-containing-excel-spreadsheets") 
    For Each Sheet In book.Sheets 
     With Sheet 
      .Range("A4", Range("A4").SpecialCells(xlLastCell)).ClearContents 
     End With 
    Next 
    book.Save 
    book.Close 

回答

2

試圖改變

.Range("A4", Range("A4").SpecialCells(xlLastCell)).ClearContents 

.Range("A4", .Range("A4").SpecialCells(xlLastCell)).ClearContents 

你已經錯過了前.Range("A4").SpecialCells(xlLastCell)等,Range("A4").SpecialCells(xlLastCell)總是試圖找到最後一個單元格中的第一張

+1

好斑點。我的意思是丟失的點。 –

+1

不能相信我爲舊的失蹤點例程而墮落:)感謝您的發現! – Damian

相關問題