在一個單獨的宏,我想選擇一個範圍,名稱,然後自動篩選數據時,請參考該範圍。這裏是我的代碼:Method對象的「範圍」「_Worksheet」失敗 - 命名範圍,並提到它在VBA
'Select and name range
Set bDataDump = ActiveWorkbook
Set DataDump = bDataDump.Sheets(1)
DataDump.Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range(Selection, Selection.End(xlDown)).Select
Set myrange = Selection
'AutoFilter that range for everything but PDC1 and delete visible rows
'Code fails on next line
DataDump.Range("myrange").AutoFilter field:=11, Criteria1:="<>""PDC1"""
ActiveCell.Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
DataDump.ShowAllData
我得到的「方法‘範圍’對象的‘_Worksheet’失敗」當我運行宏,調試器突出了代碼的第二塊作爲罪魁禍首的第一道防線。我一直沒能在這樣的過濾數據網頁上找到任何示例,所以我很感謝任何有關我在做什麼錯誤的見解。謝謝!
哇 - 更多'Select's比行代碼 - [亞洲時報Siddharth潰敗(http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select)不會批准! – pnuts
笑@ PNUTS:你讓我看起來像一個魔鬼:):@SMPerron [有趣的閱讀](http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select) –
@pnuts:哦,你已經提到了這個鏈接。糟糕... –