简体   繁体   English

VBA从一个Excel搜索到另一个Excel并显示单元格地址

[英]VBA to search from one excel to another excel and display cell address

I have two excel file. 我有两个Excel文件。 I am searching the data in another file with the unique email ID. 我正在使用唯一的电子邮件ID在另一个文件中搜索数据。 I am taking email ID from one excel file one by one and searching in another excel file. 我从一个Excel文件中一个接一个地获取电子邮件ID,然后在另一个Excel文件中搜索。 Once the data is found, I want the cell address. 一旦找到数据,我想要单元地址。 I am getting error on " MsgBox cell.Address " code as "Object variable or with block variable not set". 我在“ MsgBox cell.Address”代码上收到“对象变量或未设置块变量”的错误。 Please assist 请协助

Dim myFileNameDir As String
Dim myFileNameDir2 As String
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim emailID As String
Dim cell As Range
Dim II As Integer
Dim III As Integer

myFileNameDir2 = TextBox2.Value
Workbooks.Open Filename:=myFileNameDir2, UpdateLinks:=0
Set ws2 = Worksheets(1)

myFileNameDir = TextBox1.Value
Workbooks.Open Filename:=myFileNameDir, ReadOnly:=True, UpdateLinks:=0
Set ws = Worksheets(1)


II = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row

For III = 2 To II
emailID = ws2.Cells(III, "D").Value

Set cell = ws.Range("AA2:AA1048576").find(emailID, LookAt:=xlWhole)
MsgBox cell.Address

Next III

Most likely in this case the error is coming because the Find method is not finding a match, so there's nothing to return. 在这种情况下,最有可能出现错误,因为Find方法没有找到匹配项,因此没有任何返回值。 The Find method can be tricky to work with from VBA, especially when looking at xlWhole. 从VBA使用Find方法可能很棘手,尤其是在查看xlWhole时。 Are you sure that there's a matching value in the other sheet? 您确定另一张纸上有匹配的值吗? Are you sure the match is exact? 您确定比赛正确吗? Etc. Even merged cells can throw the Find method off. 等等,即使合并的单元格也可以抛出Find方法。

Normally using worksheet functions will slow things down, but in this case you might be best off using a MATCH function to find the matching row, then returning that value into your cell address. 通常,使用工作表函数会使速度变慢,但是在这种情况下,最好使用MATCH函数查找匹配的行,然后将该值返回到单元格地址中。

Good luck! 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA 从一个单元格中提取单元格地址,从另一个单元格中提取一个值,然后输入到另一个单元格中 - Excel VBA to pull a cell address from one cell, pull a value from another, and input into yet another cell 使用Excel VBA在一个单元格中搜索一个值,并在同一行上在另一单元格中搜索另一个值 - Use Excel VBA to Search One cell for one value and on the same line another cell for another value Excel VBA - 将单元格内容从一张纸复制到另一张纸 - Excel VBA - copying cell contents from one sheet to another Excel VBA:将单元格值从一个工作簿复制到另一个? - excel vba: copy cell value from one workbook to another? 从一个单元格中获取价值并将其集中在另一张表上 Vba Excel - Get Value from one cell and focus it on another sheet Vba Excel 如何在Excel VBA中将单元格从一行复制到另一行 - How to copy cell from one row to another in excel vba 使用vba将单元格从一个工作簿复制到excel中的另一个工作簿 - Copying the cell from One workbook to another workbook in excel using vba 一张纸上的时间戳来自另一个VBA Excel上更新的单元格 - Timestamp on one sheet from updated cell on another VBA excel Excel VBA如何将单元格内容从一本书复制到另一本书 - Excel VBA how to copy cell content from one book to another Excel VBA基于另一个字符串计算一个单元格 - Excel VBA calculate one cell based on string from another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM