呈现多个匹配数据

huangapple go评论56阅读模式
英文:

Presenting multiple match data

问题

我使用此脚本(在此处找到)并进行了一些修改,但现在未呈现找到的信息。我在单元格C14、C15和C22中都找到了短语“By Wedge”,但只打印与C14对应的信息:

Function WedgeUsadaEn()

Dim myArray() As Variant
Dim x As Long, y As Long, lNumElements As Long
Dim msg As String

With Worksheets("OBQ").Range("C13:C33")
     Set c = .Find("By Wedge", LookIn:=xlValues)
     If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            ReDim Preserve myArray(y)
            myArray(y) = c.Offset(0, -2).Value
            y = y + 1
            Set c = .FindNext(c)
        If c Is Nothing Then
            GoTo DoneFinding
        End If
        Loop While c.Address <> firstAddress
      End If
DoneFinding:
End With

lNumElements = UBound(myArray) - LBound(myArray)

If lNumElements = 1 Then
msg = myArray(0)
Else
    For x = LBound(myArray) To UBound(myArray)
        If x < (lNumElements - 1) Then
            msg = msg & myArray(x) & ", "
        Else
            msg = msg & myArray(x)
        End If
    Next x
End If

WedgeUsadaEn = msg

End Function

我希望有人能为我解释发生了什么事情。

如果我将它保留为子例程而不是函数,就像原来的那样,所有数据都会正确显示。

英文:

Screenshot I use this script (found here) and did a little of modification, but now is not presenting the information found, I got the phrase "By Wedge" in the cells C14, C15 and C22, but only prints the info corresponding to C14:

Function WedgeUsadaEn()

Dim myArray() As Variant
Dim x As Long, y As Long, lNumElements As Long
Dim msg As String

With Worksheets(&quot;OBQ&quot;).Range(&quot;C13:C33&quot;)
     Set c = .Find(&quot;By Wedge&quot;, LookIn:=xlValues)
     If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            ReDim Preserve myArray(y)
            myArray(y) = c.Offset(0, -2).Value
            y = y + 1
            Set c = .FindNext(c)
        If c Is Nothing Then
            GoTo DoneFinding
        End If
        Loop While c.Address &lt;&gt; firstAddress
      End If
DoneFinding:
End With

lNumElements = UBound(myArray) - LBound(myArray)

If lNumElements = 1 Then
msg = myArray(0)
Else
    For x = LBound(myArray) To UBound(myArray)
        If x &lt; (lNumElements - 1) Then
            msg = msg &amp; myArray(x) &amp; &quot;, &quot;
        Else
            msg = msg &amp; myArray(x)
        End If
    Next x
End If

WedgeUsadaEn = msg

End Function

I hope anyone can bring me some light about what is this happening

If I keep it as sub and not a function, like the original, all the data is presente properly

答案1

得分: 1

Function WedgeUsadaEn()

    Dim c As Range, msg As String, sep As String, firstAddress As String
    
    With Worksheets("OBQ").Range("C13:C33")
         Set c = .Find("By Wedge", LookIn:=xlValues)
         If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                msg = msg & sep & c.Offset(0, -2).Value
                sep = ", "
                Set c = .FindNext(c)
            Loop While c.Address <> firstAddress
          End If
    End With
DoneFinding:
    
    WedgeUsadaEn = msg

End Function
英文:

Simpler:

Function WedgeUsadaEn()

    Dim c As Range, msg As String, sep as string, firstAddress As String
    
    With Worksheets(&quot;OBQ&quot;).Range(&quot;C13:C33&quot;)
         Set c = .Find(&quot;By Wedge&quot;, LookIn:=xlValues)
         If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                msg = msg &amp; sep &amp; c.Offset(0, -2).Value
                sep = &quot;, &quot;
                Set c = .FindNext(c)
            Loop While c.Address &lt;&gt; firstAddress
          End If
    End With
DoneFinding:
    
    WedgeUsadaEn = msg

End Function

huangapple
  • 本文由 发表于 2023年2月27日 03:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定