英文:
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("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
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("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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论