[英]Excel VBA Find function not returning an integer value, can't see what I am doing wrong
so I am writing an excel marco to automate some manual tasks. 所以我正在写一个excel marco来自动化一些手动任务。 I've hit a wall in a function I am writing, it fails to return a integer value to me for use in the sub I am calling it from.
我在编写的函数中碰壁,它无法向我返回一个整数值以供我从中调用它的子函数使用。 I feel like this is simple but I can't for the life of me see what I am doing wrongly.
我觉得这很简单,但是我一生都看不到自己做错了什么。
Function Find_Header(SearchTerm As String) As Integer
Dim Rng As Range
With Sheets("DATA").Range("A:CZ")
Set Rng = .Find(What:=SearchTerm, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Find_Header = Rng.Column
Else
MsgBox "Nothing found"
End If
End With
End Function
From witin a sub I call it thusly: 因此,我从一个潜水艇上这样称呼它:
Find_Header SearchTerm:="Status"
And since the term 'Status' is within the range specific and is Column number 5 I expect to see 'Find_Header' is equal to 5 within my sub, however as soon as the function ends the variable is lost, what I am not doing to retain this value in the sub routine? 并且由于术语“状态”在特定范围内并且是第5列,因此我希望在我的子程序中看到“ Find_Header”等于5,但是一旦函数结束,变量就丢失了,我在做什么在子例程中保留此值?
Thanks, Chris 谢谢克里斯
You need to store the result in a variable - for example: 您需要将结果存储在变量中-例如:
Dim iFoundCol as Integer
iFoundCol = Find_Header(SearchTerm:="Status")
函数返回一个需要存储在变量中的值,您不能只是简单地调用该函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.