2016-08-19 36 views
1

我有一個代碼,可能有一個「蘋果 - 總計」的實例,但它非常罕見,而總是會有一個「蘋果」實例。我怎麼能創建一個代碼來檢查字符串是否存在於一行中?目前的問題是,如果代碼不在那裏,代碼就會出錯。如果有「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 
+0

O n您需要的錯誤轉到和標籤,或者先檢查,如果存在,則計數。 http://www.cpearson.com/excel/errorhandling.htm –

+2

任何你沒有使用'.Find'的理由? –

+0

如果您只是想忽略該錯誤,則可以在聲明變量下面添加語句「On Error Resume Next」。 –

回答

1

您還可以使用:

如果ISERROR(application.match)...和處理它的方式

0
On error goto TryApple 

' try for total and goto eHandle if found 

TryApple: 

On error goto eHandle 

' try for Apple 

eHandle: 

的第一次嘗試是總喜歡嘗試,TryApple是像catch和eHandle是默認

2

由於:

  • 它總是更好:

    • 避免On Error Resume Next方法

      這是相當危險的,應該被限制在極少數情況下(如檢查任何集合元素)

    • 使用Match()Application對象的功能而不是WorksheetFunction對象

      因爲它將陷阱陷入它的返回值,因此不會在可能Match()失敗

  • 停止執行代碼假設:

    • 要存儲到cur行中的值正確的列下

    • "Apple""Fund"是兩個字符串文字而不是字符串變量

第一種方法,更緊密你的下面,可能是以下幾點:

Option Explicit 

Sub main() 
    Dim letr As Variant 
    Dim cur As Double 

    With Sheets("SOFP").Range("2:2") '<-- reference your worksheet row 2 
     If Not IsError(Application.Match("Apple-Total", .Cells, 0)) Then '<-- if there's "Apple-Total"... 
      letr = Application.Match("Fund-Total", .Cells, 0) '<-- ...then try finding "Fund-Total" 
     ElseIf Not IsError(Application.Match("Apple", .Cells, 0)) Then '<-- otherwise if there's "Apple"... 
      letr = Application.Match("Fund", .Cells, 0) '<-- ...then try finding "Fund" 
     End If 

     If Not IsError(letr) Then '<-- if the "proper Fund" has been succesfully found... 
      letr = Split(Cells(, letr).Address, "$")(1) '<-- ...then get "proper Fund" column 
      cur = Trim(.Range(letr & "2").Value) '<-- and return the value in the 3rd row (i.e. with a row index of 2 with reference to row "2") 
     End If 
    End With 
End Sub 

但你可能要考慮下面的 「查找()」 的方法:

Option Explicit 

Sub main2() 
    Dim f As Range 
    Dim cur As Double 

    With Sheets("SOFP").Range("2:2") '<-- reference your worksheet row 2 
     If Not .Find(what:="Apple-Total", LookIn:=xlValues, lookAt:=xlWhole, MatchCase:=False) Is Nothing Then '<-- if "Apple-Total" has been found ... 
      Set f = .Find(what:="Fund-Total", LookIn:=xlValues, lookAt:=xlWhole, MatchCase:=False) '<-- ...then try finding "Fund-Total" 
     ElseIf Not .Find(what:="Apple", LookIn:=xlValues, lookAt:=xlWhole, MatchCase:=False) Is Nothing Then '<-- otherwise, if "Apple" has been found ... 
      Set f = .Find(what:="Fund", LookIn:=xlValues, lookAt:=xlWhole, MatchCase:=False) '<-- ...then try finding "Fund" 
     End If 
     If Not f Is Nothing Then cur = Trim(f.Offset(1).Value) '<-- if the "proper Fund" has been succesfully found then store the value in row 3 of its corresponding column 
    End With 
End Sub 

我覺得很整齊

+0

我同意。 '發現'是很整齊 –

+0

@Lowpar:你通過了嗎? – user3598756

相關問題