[英]can't find email string using Find method in excel vba
I'm trying to find the address of the cell containing a specific email address. 我正在尝试查找包含特定电子邮件地址的单元格的地址。 here's my code:
这是我的代码:
Function find_email(range_input As Range) As String
Dim item_input As String
item_input = "test@test.com"
Dim found_range As Range
With Worksheets("test_sheet").range_input
Set found_range = .Find(What:=item_input, LookIn:=xlValues, LookAt:=xlPart)
End With
find_email = found_range.Address
End Function
EDIT post answer 1 编辑帖子答案1
I've taken the suggestion made in answer #1 and changed to code to be: 我采纳了答案1中的建议,并将代码更改为:
Public Function find_email(range_input As Range, string_input As String, collumn_offsett As Integer) As String
Dim found_range As Range
With Worksheets("test_sheet").Range(range_input.Address)
Set found_range = .Find(What:=string_input, LookIn:=xlValues, LookAt:=xlPart)
End With
Dim output_range As Range
Set output_range = found_range.Offset(0, collumn_offsett)
find_email = output_range
End Function
Sub test_email_find_code()
Debug.Print find_email(Range("f1:f7"), "test@test.com", -3)
End Sub
The sub test_email_find_code
works great. 子
test_email_find_code
效果很好。 When I try to use function find_email
in a cell, however, it doesn't work. 但是,当我尝试在单元格中使用函数
find_email
时,它不起作用。 I don't really understand how to research this. 我真的不知道如何研究这个。 Do I have to adjust the range input somehow?
我是否必须以某种方式调整范围输入?
To make it work like you have written you'll need to change the With line like this: 要使其像您编写的那样工作,您需要像下面这样更改With行:
With Worksheets("test_sheet").Range(range_input.Address)
Here's your full code located in a separate module: 这是您的完整代码,位于单独的模块中:
Public Function find_email(range_input As Range) As String
Dim item_input As String
item_input = "test@test.com"
Dim found_range As Range
With Worksheets("test_sheet").Range(range_input.Address)
Set found_range = .Find(What:=item_input, LookIn:=xlValues, LookAt:=xlPart)
End With
find_email = found_range.Address
End Function
You can test it with another sub like this: 您可以使用另一个子测试如下:
Sub findEmail()
Debug.Print find_email(Range("A1:D100"))
End Sub
Or use an in-cell function like this: 或使用像这样的单元内函数:
=find_email(A1:D100)
Result depends on the answer, I used cell C4 so the result was $C$4
结果取决于答案,我使用单元格C4,因此结果为
$C$4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.