[英]how do I get a cell's location as a range in excel vba?
i'm trying to lookup an email in a column, then use that email's location to find other information in the relevant row. 我正在尝试在列中查找电子邮件,然后使用该电子邮件的位置在相关行中查找其他信息。 here's my code so far:
到目前为止,这是我的代码:
Dim wsUsers As Worksheet
Set wsUsers = Worksheets("userOutput.csv")
Dim userRows As Integer
userRows = wsUsers.Cells(Rows.Count, 1).End(xlUp).Row
Dim userEmail As Range
Dim userEmailColumn As Range
Set userEmailColumn = wsUsers.Range(Cells(2, 4), Cells(userRows, 4))
Dim findEmail As Variant
Set findEmail = userEmailColumn.Find("test@test.com")
Dim userEmailAd As Range
Set userEmailAd = findEmail.Address
MsgBox userEmailAd
stop
I'm getting runtime error 424, object required
and it highlights the line: Set userEmailAd = findEmail.Address
我收到
runtime error 424, object required
,并且突出显示了以下行: Set userEmailAd = findEmail.Address
How do I get a range so I can then use offset to find other information in the row? 我如何获得一个范围,然后可以使用偏移量在行中查找其他信息? Or, is there a better way?
或者,还有更好的方法? I'm not using vlookup because column is not the first one in the overall range.
我不使用vlookup,因为column不在整个范围中的第一列。
Why are you making the findEmail variable a variant, then just assigning it to a new variable that holds the range? 为什么要使findEmail变量成为变量,然后将其分配给一个包含范围的新变量?
You are getting the error because the .Address property of a range is a string. 由于范围的.Address属性是字符串,因此出现错误。 Do you want userEmailAd to be a range (object) or a string that names the range (string)?
您想将userEmailAd作为范围(对象)还是将范围命名为字符串(字符串)? Based on your question, you want to make it a range, so I'd change it to this:
根据您的问题,您想使其成为一个范围,因此我将其更改为:
Dim wsUsers As Worksheet
Set wsUsers = Worksheets("userOutput.csv")
Dim userRows As Integer
userRows = wsUsers.Cells(Rows.Count, 1).End(xlUp).Row
Dim userEmail As Range
Dim userEmailColumn As Range
Set userEmailColumn = wsUsers.Range(Cells(2, 4), Cells(userRows, 5))
Dim findEmail As Range
Set findEmail = userEmailColumn.Find("test@test.com")
Dim userEmailAd As Range
Set userEmailAd = findEmail
MsgBox userEmailAd.Address
Stop
userRows = wsUsers.Cells(Rows.Count, 1).End(xlUp).Row
Set userEmailColumn = wsUsers.Range(Cells(2, 4), Cells(userRows, 4))
should rather be: 应该是:
userRows = wsUsers.Cells(wsUsers.Rows.Count, 1).End(xlUp).Row
Set userEmailColumn = wsUsers.Range(wsUsers.Cells(2, 4), wsUsers.Cells(userRows, 4))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.