测试以查看字符串是否在一行中

时间:2016-08-19 11:23:15

标签: vba excel-vba excel

我有一个代码,其中可能有一个" Apple - Total"的实例,但它非常罕见,而且总会有一个" Apple"的实例。我怎么能创建一个代码来检查字符串是否存在于一行中?目前的问题是,如果代码不存在,代码就会出错。如果有一个实例" Apple - Total"它应该优先于#34; 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

3 个答案:

答案 0 :(得分:2)

自:

  • 它总是更好:

    • 避免On Error Resume Next接近

      这是非常危险的,应该限于极少数情况(比如检查任何收集元素)

    • 使用Match()对象的Application函数代替WorksheetFunction对象

      因为它将错误捕获到其返回值中,因此在可能的Match()失败时不会停止执行代码

  • 假设:

    • 您希望将cur存储在正确列下方行中的值

    • "Apple""Fund"是两个String 文字而不是String 变量

第一种方法,更贴近您的方法,可能如下:

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

但您可能需要考虑以下内容&#34; Find()&#34;的方法:

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

我认为更整洁

答案 1 :(得分:1)

你也可以使用:

如果iserror(application.match)......并以这种方式处理

答案 2 :(得分:0)

On error goto TryApple

'  try for total and goto eHandle if found

TryApple:

On error goto eHandle

' try for Apple

eHandle:

第一次尝试总计就像Try,TryApple就像catch,而eHandle是默认的