繁体   English   中英

Excel VBA Find函数未返回整数值,看不到我在做什么

[英]Excel VBA Find function not returning an integer value, can't see what I am doing wrong

所以我正在写一个excel marco来自动化一些手动任务。 我在编写的函数中碰壁,它无法向我返回一个整数值以供我从中调用它的子函数使用。 我觉得这很简单,但是我一生都看不到自己做错了什么。

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

因此,我从一个潜水艇上这样称呼它:

Find_Header SearchTerm:="Status"

并且由于术语“状态”在特定范围内并且是第5列,因此我希望在我的子程序中看到“ Find_Header”等于5,但是一旦函数结束,变量就丢失了,我在做什么在子例程中保留此值?

谢谢克里斯

您需要将结果存储在变量中-例如:

Dim iFoundCol as Integer
iFoundCol = Find_Header(SearchTerm:="Status")

函数返回一个需要存储在变量中的值,您不能只是简单地调用该函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM