gpt4 book ai didi

excel - VBA 函数未执行

转载 作者:行者123 更新时间:2023-12-04 21:38:14 27 4
gpt4 key购买 nike

我正在尝试创建一个在给定单元格范围内查找值的函数,但它没有被执行。它应该返回找到值的单元格。

Function searchInRange(where As Range, what As String) As Range
For Each c In where.Cells
Debug.Print (c.Value)
If c.Value = what Then
searchInRange = c
End If
Next c

End Function

我从一个子模块调用它,但它没有被执行。如果我去调试调试器只是跳转到下一条语句而不输入函数。我不明白为什么

编辑

正如许多人建议的那样,我发布了调用函数的模块。它是一个按钮单击功能。正确选择了文件和工作表。我知道这一点是因为最后一句话(PE_Sheet.activate)工作正常。问候。
Private Sub CommandButton1_Click()
Dim PE_File As Workbook
Dim PE_Sheet As Worksheet
Dim cell As Range


Set PE_File = Workbooks(getSelectedWorkbook())
Set PE_Sheet = PE_File.Worksheets("Monitored")
Unload UserForm1

searchAlarmFilter PE_Sheet.Range("A:A"), "5184"

PE_Sheet.Activate
End Sub

最佳答案

您还需要set范围

Public Function sir(where As Range, what As String) As Range
Dim res As Range: Set res = Nothing
For Each c In where.Cells
Debug.Print c.Value
If c.Value = what Then
Set res = c
Exit For
End If
Next
Set sir = res
End Function

关于excel - VBA 函数未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com