简体   繁体   English

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

[英]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.

相关问题 我对这个 Excel vba 做错了什么 - What am I doing wrong with this Excel vba 我究竟做错了什么? 使用Excel VBA删除重复项 - What am I doing wrong? Removing duplicates using Excel VBA 我的 VBA Vlookup 返回 N/A。 我究竟做错了什么? - My VBA Vlookup is returning N/A. What am I doing wrong? 在Excel VBA宏中创建一个新行(我做错了什么?) - Creating a new row in Excel VBA macro (what am I doing wrong?) 我正在使用VBA DateSerial函数分隔字符串中的日期,但是当字符串中的年份为'1000'时,Excel Ends Sub会出现。 我究竟做错了什么? - I am using the VBA DateSerial function to separate dates in a string, but Excel Ends Sub when year in string is '1000'. What am I doing wrong? 尝试并无法通过VBA在Excel中的列中循环。 我究竟做错了什么? - Trying and failing to loop through a column in Excel with VBA. What am I doing wrong? Excel VBA:在for循环中跳转:“ Next for For” –我在做什么错? - Excel VBA: Jump inside a for loop: “Next without For” – What am I doing wrong? VBA创建数据透视表,类型不匹配? 我究竟做错了什么? - VBA to Create PivotTable, Type Mismatch? What Am I Doing Wrong? VBA将多列转换为两列-我在做什么错? - VBA Transform Many Columns to Two - What am I doing wrong? 我在excel行循环的拆分中做错了什么? - What am I doing wrong in this split for excel rows loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM