2016-08-19 7 views
1

「Apple - Total」のインスタンスが存在する可能性があるコードがありますが、「Apple」のインスタンスが常に存在するのに対し、非常にまれです。文字列が行内に存在するかどうかをチェックするコードをどのように作成できますか?問題は現時点では、コードが存在しない場合にバグが出るということです。 "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あなたが必要とするエラーやラベル、または最初にチェックしてください。 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ですキャッチのような、そしてeHandleがあるので、デフォルト

2

です:

  • それは常に良いでしょう:非常に危険であり、非常に少数の例に限定されるべきである

    • 回避On Error Resume Nextアプローチ

      (任意のコレクション要素をチェックしたい)

    • 使用Match()関数の代わりにApplicationオブジェクトの機能

      エラーをトラップするのでしたがって、その戻り値とは仮定可能Match()失敗

  • 時にコードの実行を停止しません:

    • は、あなたが正しい列の下curに行の値を格納したい

    • "Apple""Fund"は2つの文字列リテラルで、ストリングではない変数

    • より密接にあなたの次

最初のアプローチは、次のようになります。

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

私は同意します。 '.Find'ははるかにneaterです –

+0

@Lowpar:あなたはそれを通過しましたか? – user3598756

関連する問題