取消时vba excel文本框中的错误

时间:2013-07-29 04:47:57

标签: excel vba excel-vba

Sub search()

Columns("J:K").Select
Selection.EntireColumn.Hidden = False

ActiveSheet.PivotTables("PivotTable15").PivotSelect "device[All]", xlLabelOnly _
    + xlFirstRow, True

Cells.Find(What:=InputBox("Please enter the PartID", "Search"), _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

ActiveCell.Offset(0, 1).Select
Selection.ShowDetail = True

Selection.Copy
Sheets("interface").Select
Range("L501").Select
ActiveSheet.Paste

Columns("J:K").Select
Selection.EntireColumn.Hidden = True
Range("A2").Select




 End Sub

我的子按钮上有这个代码但是当我取消显示的输入框时出现错误..错误是:“无法设置Range类的ShowDetail属性”。突出显示的错误是:

 Selection.ShowDetail = True

1 个答案:

答案 0 :(得分:0)

错误的来临是因为它找不到任何东西。您可以通过多种方式捕获错误,其中之一是:

Sub search()
dim sPartID as string
Columns("J:K").Hidden = False

ActiveSheet.PivotTables("PivotTable15").PivotSelect "device[All]", xlLabelOnly _
    + xlFirstRow, True

sPartID=InputBox("Please enter the PartID", "Search")
if len(sPartID)=0 then exit sub

Cells.Find(What:=sPartID, _
After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

ActiveCell.Offset(0, 1).ShowDetail = True

Selection.Copy Sheets("interface").Range("L501")

Columns("J:K").EntireColumn.Hidden = True
Range("A2").Select

End Sub