函数调用后查找下一个
Findnext after function call
当我调用我的函数时(你可以看到我评论的行),它不会循环,因为 .FindNext(Cell)
returns nothing
。如果我删除该行。它循环了适当的数量,但我无法获取字符串,我想追加。在我看来,这像是某种按价值调用的问题。有人有想法吗?
这是我的代码:
Sub NachInhaltSuchen()
Dim result As String
result = ""
For Each ws In ActiveCell.EntireRow
With ws.Cells
Set Cell = .Find(what:="x", LookAt:=xlValues)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Dim shortName As String
shortName = Cells(1, Cell.Column)
Dim semName As String
semName = SeminarName(shortName) 'If you remove this Line, it loops the appropriate amount of times
result = result & shortName & ": " '& semName - this should be re-added once it works
Set Cell = .FindNext(Cell)
If Cell Is Nothing Then Exit Do
Loop Until Cell.Address = FirstAddress
End If
End With
Next ws
MsgBox result
End Sub
Public Function SeminarName(ByVal name As String) As String
Dim longName As String
longName = ""
Dim row As Integer
With Worksheets("Seminare")
Set c = .Cells(1).EntireColumn.Find(what:=name)
If Not c Is Nothing Then
longName = .Cells(c.row, 2)
End If
End With
SeminarName = longName
End Function
您不能将一个 Find
嵌套在另一个 Find
中,然后使用 FindNext
重复第一个 Find
。由于您的函数基本上只是一个查找,因此您可以改用 Vlookup
:
Public Function SeminarName(ByVal name As String) As String
Dim longName As String
longName = ""
Dim match
match = application.vlookup(name, Worksheets("Seminare").Range("A:B"), 2, false)
If Not iserror(match) Then longName = match
SeminarName = longName
End Function
当我调用我的函数时(你可以看到我评论的行),它不会循环,因为 .FindNext(Cell)
returns nothing
。如果我删除该行。它循环了适当的数量,但我无法获取字符串,我想追加。在我看来,这像是某种按价值调用的问题。有人有想法吗?
这是我的代码:
Sub NachInhaltSuchen()
Dim result As String
result = ""
For Each ws In ActiveCell.EntireRow
With ws.Cells
Set Cell = .Find(what:="x", LookAt:=xlValues)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Dim shortName As String
shortName = Cells(1, Cell.Column)
Dim semName As String
semName = SeminarName(shortName) 'If you remove this Line, it loops the appropriate amount of times
result = result & shortName & ": " '& semName - this should be re-added once it works
Set Cell = .FindNext(Cell)
If Cell Is Nothing Then Exit Do
Loop Until Cell.Address = FirstAddress
End If
End With
Next ws
MsgBox result
End Sub
Public Function SeminarName(ByVal name As String) As String
Dim longName As String
longName = ""
Dim row As Integer
With Worksheets("Seminare")
Set c = .Cells(1).EntireColumn.Find(what:=name)
If Not c Is Nothing Then
longName = .Cells(c.row, 2)
End If
End With
SeminarName = longName
End Function
您不能将一个 Find
嵌套在另一个 Find
中,然后使用 FindNext
重复第一个 Find
。由于您的函数基本上只是一个查找,因此您可以改用 Vlookup
:
Public Function SeminarName(ByVal name As String) As String
Dim longName As String
longName = ""
Dim match
match = application.vlookup(name, Worksheets("Seminare").Range("A:B"), 2, false)
If Not iserror(match) Then longName = match
SeminarName = longName
End Function