我有一個代碼,可能有一個「蘋果 - 總計」的實例,但它非常罕見,而總是會有一個「蘋果」實例。我怎麼能創建一個代碼來檢查字符串是否存在於一行中?目前的問題是,如果代碼不在那裏,代碼就會出錯。如果有「Apple - Total」的實例,它應該優先於「Apple」。像R中的Try函數就可以工作。測試一個字符串是否連續
If WorksheetFunction.Match(Apple & "-Total", Sheets("SOFP").Range("2:2"), 0) > 0 Then
letr = WorksheetFunction.Match(Fund & "-Total", Sheets("SOFP").Range("2:2"), 0)
letr = Split(Cells(, letr).Address, "$")(1)
cur = Sheets("SOFP").Offset(1, 0).Value
ElseIf WorksheetFunction.Match(Apple , Sheets("SOFP").Range("2:2"), 0) > 0 Then
letr = WorksheetFunction.Match(Fund, Sheets("SOFP").Range("2:2"), 0)
letr = Split(Cells(, letr).Address, "$")(1)
cur = Trim(Sheets("SOFP").Offset(1, 0).Value)
End If
O n您需要的錯誤轉到和標籤,或者先檢查,如果存在,則計數。 http://www.cpearson.com/excel/errorhandling.htm –
任何你沒有使用'.Find'的理由? –
如果您只是想忽略該錯誤,則可以在聲明變量下面添加語句「On Error Resume Next」。 –