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