2016-08-19 38 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

相关问题