我正在尝试在列中查找电子邮件,然后使用该电子邮件的位置在相关行中查找其他信息。这是我目前的代码:
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
我收到runtime error 424, object required
并突出显示该行:Set userEmailAd = findEmail.Address
如何获取范围,以便我可以使用偏移量来查找行中的其他信息?或者,还有更好的方法?我没有使用vlookup,因为列不是整个范围中的第一个。
答案 0 :(得分:0)
为什么要将findEmail变量变为变量,然后将其分配给保存范围的新变量?
您收到错误,因为范围的.Address属性是字符串。您是否希望userEmailAd成为范围(对象)或命名范围(字符串)的字符串?根据您的问题,您希望将其设为范围,因此我将其更改为:
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
答案 1 :(得分:0)
userRows = wsUsers.Cells(Rows.Count, 1).End(xlUp).Row
Set userEmailColumn = wsUsers.Range(Cells(2, 4), Cells(userRows, 4))
应该是:
userRows = wsUsers.Cells(wsUsers.Rows.Count, 1).End(xlUp).Row
Set userEmailColumn = wsUsers.Range(wsUsers.Cells(2, 4), wsUsers.Cells(userRows, 4))