简体   繁体   English

如何在Excel VBA中获取单元格的位置范围?

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

相关问题 如何在循环显示一系列单元格时获取当前单元格编号(Excel VBA) - How do I get the current cell number whilst looping through a range of cells (Excel VBA) 如何在Excel VBA中引用单元格的位置 - how to reference a cell's location in excel VBA 如何在VBA excel中获取合并单元格的范围? - How to get range of merged cell in VBA excel? 如何在 Excel VBA 中获取包含工作表名称但不包含工作簿名称的范围地址? - How do I get a range's address including the worksheet name, but not the workbook name, in Excel VBA? Excel VBA:更改单元格后,如何将SpecialSpecial粘贴到单元格位置的同一行? - Excel VBA: How do I PasteSpecial into same row of Cell location after Cell is changed? 如何使用VBA在excel单元中获取月份和日期? - How do I get the month and day in excel cell using VBA? 如何在 Excel VBA 中获取已更改单元格的旧值? - How do I get the old value of a changed cell in Excel VBA? 如何将 Excel 公式 =STDEV.S(IF(FREQUENCY(range,range),range)) 转换为 VBA 代码? - How do I convert the Excel formula =STDEV.S(IF(FREQUENCY(range,range),range)) into VBA code? 如何在一个范围内获得单元格的位置? - How do I get a cell's position within a range? 在Excel VBA中,如何更改作为用户输入传递给函数的单元格区域 - In Excel VBA, How do I change the cell range that was passed as a user input to a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM