2013-11-04 77 views
1

我想從Excel工作簿的CoverPage工作表中獲得選擇執行的樂趣。它打算在下一頁選擇一個單元格範圍並執行任何操作。範圍()。選擇和錯誤1004問題

ThisWorkbook.Sheets("Purpose").Range(Cells(48, 1), Cells(48, 9)).Select 

With Selection 
    .Borders (xlEdgeBottom) 
    .LineStyle = xlContinuous 
    .ColorIndex = 0 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 

但是,當我打開「目的」表時,代碼就會運行。但是一旦我從封面上運行它就會失敗。它與我猜測的引用有關,但是我嘗試了其他解決方案,我可以找到並且它不起作用。

Anyhelp是apprciated, 史蒂芬

+0

使用'Select'方法時常見的問題範圍。避免它的方法:除非絕對必要,否則不要使用「選擇」。 99%的時間可以在沒有它的情況下完成。 –

+2

[有趣的閱讀](http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select) –

+0

感謝您的鏈接。現在閱讀! – Alex

回答

2

如果工作表名稱Purpose不活躍,當您運行宏,你會得到一個錯誤,因爲Excel無法Select在非活動工作表範圍。

修訂:

With ThisWorkbook.Sheets("Purpose").Range(Cells(48, 1), Cells(48, 9)) 
    .Borders (xlEdgeBottom) 
    .LineStyle = xlContinuous 
    .ColorIndex = 0 
    .TintAndShade = 0 
    .Weight = xlThin 
End With 
+0

感謝您的幫助! – Alex