簡體   English   中英

Excel-VBA Range.Find方法錯誤處理

[英]Excel-VBA Range.Find method error handling

我正在使用range.find方法在第一行中找到具有特定值的單元格。 匹配時,代碼可以正常工作。 如果沒有匹配項,則代碼將引發錯誤,如下面的屏幕快照所示。

我們需要為此方法進行錯誤處理嗎? 我以為它只會返回Null或Nothing。 謝謝您的幫助!

在此處輸入圖片說明

用這個。 如果您直接想返回一個空范圍的列號,它將顯示錯誤。

Sub findtest() 
dim c as long
dim rng as range
   Set rng = ActiveSheet.Rows("1:1").Find(What:="John Smith", _
        After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, _
        SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    If Not rng Is Nothing Then c = rng.column : Debug.Print c
end sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM