You are here:

Excel/Compare find and fill the surched data in desired field

Advertisement


Question
QUESTION: Hey Tom ! How are you my excel God. Glad to send u a question again.
I have to excel work books both with different data fields but for same department.

i want a macro that will pick a data from first work book from
"account _number" field and search the data in second work book and if finds the searched data copy the value from second workbook's "Date first bill issued" and paste the value in first workbook's "Date first bill issued".

if needed i can send you both work books.

Thanks & Regards
Amit Rajput

ANSWER: Amit,

Test this on a copy of your workbook.  Update the code to reflect your actual workbook and sheet names.  

Test it on a copy of your workbooks.

The code worked for me in my tests.


Sub ABC()
Dim bk1 As Workbook, bk2 As Workbook
Dim sh1 As Worksheet, sh2 As Worksheet
Dim r1A As Range, r1B As Range
Dim r2A As Range, r2B As Range
Dim r1 As Range, r2 As Range
Dim s1A As String, s1B As String
Dim s2A As String, s2B As String
Dim cell1A As Range, cell1B As Range
Dim cell2A As Range, cell2B As Range
s1A = "account_number"
s2A = "account_number"
s1B = "Date first bill issued"
s2B = "Date first bill issued"
Set bk1 = Workbooks("Myfirstworkbook.xls") '<-- update for actual name
Set bk2 = Workbooks("Mysecondworkbook.xls") '<-- update for actual name
'Set bk1 = Workbooks("test1.xlsm") '<-- used for testing
'Set bk2 = Workbooks("test2.xlsm") '<-- used for testing

Set sh1 = bk1.Worksheets("Sheet1") '<-- update for actual name
Set sh2 = bk2.Worksheets("Sheet1") '<-- update for actual name

Set r1 = sh1.UsedRange
Set r2 = sh2.UsedRange

Set r1A = r1.Find(What:=s1A, _
After:=r1(r1.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r1B = r1.Find(What:=s1B, _
After:=r1(r1.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r2A = r2.Find(What:=s2A, _
After:=r2(r2.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r2B = r2.Find(What:=s2B, _
After:=r2(r2.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If r1A Is Nothing Or r1B Is Nothing Or _
  r2A Is Nothing Or r2B Is Nothing Then
  MsgBox "Unable to locate at least one of the specified columns"
  Exit Sub
End If

Set r1A = r1A.Offset(1, 0).Resize(sh1.Cells(sh1.Rows.Count, r1A.Column).End(xlUp).Row - r1A.Row, 1)
Set r1B = r1B.Offset(1, 0).Resize(r1A.Rows.Count, 1)
Set r2A = r2A.Offset(1, 0).Resize(sh2.Cells(sh2.Rows.Count, r2A.Column).End(xlUp).Row - r2A.Row, 1)
Set r2B = r2B.Offset(1, 0).Resize(r2A.Rows.Count, 1)


For Each cell1A In r1A
 Set cell1B = Intersect(cell1A.EntireRow, r1B)
 Set cell2A = r2A.Find(What:=cell1A, _
         After:=r2A(r2A.Count), _
         LookIn:=xlFormulas, _
         LookAt:=xlWhole, _
         SearchOrder:=xlByRows, _
         SearchDirection:=xlNext, _
         MatchCase:=False)
 If Not cell2A Is Nothing Then
     Set cell2B = Intersect(cell2A.EntireRow, r2B)
     cell2B.Copy cell1B
 Else
     cell1B.Value = "N/A"
 End If
Next

End Sub

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom ! Thanks for replying so soon..

I copied and updated the codes as desired with a button but its showing... " Unable to locate at least one of the specified columns" error.

I would request you to kindly allow me to upload both workbooks so that i can get your help.

Thanks & Regards
Amit

ANSWER: Amit,

If you can't figure it out, send the files to me.

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom ! my excel god ! how r u ?  As a previous question the solution you provided were absolutely working but now i am facing one more problem where all the condition is same but i want one more field to add in
"first bill amount" .

Can you please edit the previous code that can do as requested above.

Thanks & Regards
Amit Rajput

Answer
Amit Rajput,

this would be my guess:

r1 As Range, r2 As Range
Dim s1A As String, s1B As String, s1C As String
Dim s2A As String, s2B As String, s2C As String
Dim cell1A As Range, cell1B As Range, cell1C As Range
Dim cell2A As Range, cell2B As Range, cell2C As Range
s1A = "account_number"
s2A = "account_number"
s1B = "Date first bill issued"
s2B = "Date first bill issued"
s1C = "first bill amount"
s2C = "first bill amount"
Set bk1 = Workbooks("Myfirstworkbook.xls") '<-- update for actual name
Set bk2 = Workbooks("Mysecondworkbook.xls") '<-- update for actual name
'Set bk1 = Workbooks("test1.xlsm") '<-- used for testing
'Set bk2 = Workbooks("test2.xlsm") '<-- used for testing

Set sh1 = bk1.Worksheets("Sheet1") '<-- update for actual name
Set sh2 = bk2.Worksheets("Sheet1") '<-- update for actual name

Set r1 = sh1.UsedRange
Set r2 = sh2.UsedRange

Set r1A = r1.Find(What:=s1A, _
After:=r1(r1.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r1B = r1.Find(What:=s1B, _
After:=r1(r1.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r1C = r1.Find(What:=s1C, _
After:=r1(r1.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r2A = r2.Find(What:=s2A, _
After:=r2(r2.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r2B = r2.Find(What:=s2B, _
After:=r2(r2.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Set r2C = r2.Find(What:=s2C, _
After:=r2(r2.Count), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If r1A Is Nothing Or r1B Is Nothing Or r1C Is Nothing Or _
  r2A Is Nothing Or r2B Is Nothing Or r2C Is Nothing Then
  MsgBox "Unable to locate at least one of the specified columns"
  Exit Sub
End If

Set r1A = r1A.Offset(1, 0).Resize(sh1.Cells(sh1.Rows.Count, r1A.Column).End(xlUp).Row - r1A.Row, 1)
Set r1B = r1B.Offset(1, 0).Resize(r1A.Rows.Count, 1)
Set r1C = r1C.Offset(1, 0).Resize(r1A.Rows.Count, 1)
Set r2A = r2A.Offset(1, 0).Resize(sh2.Cells(sh2.Rows.Count, r2A.Column).End(xlUp).Row - r2A.Row, 1)
Set r2B = r2B.Offset(1, 0).Resize(r2A.Rows.Count, 1)
Set r2C = r2C.Offset(1, 0).Resize(r2A.Rows.Count, 1)

For Each cell1A In r1A
 Set cell1B = Intersect(cell1A.EntireRow, r1B)
 Set cell1C = Intersect(cell1A.EntireRow, r1C)
 Set cell2A = r2A.Find(What:=cell1A, _
         After:=r2A(r2A.Count), _
         LookIn:=xlFormulas, _
         LookAt:=xlWhole, _
         SearchOrder:=xlByRows, _
         SearchDirection:=xlNext, _
         MatchCase:=False)
 If Not cell2A Is Nothing Then
     Set cell2B = Intersect(cell2A.EntireRow, r2B)
     Set cell2C = Intersect(cell2A.EntireRow, r2C)
     cell2B.Copy cell1B
     cell2C.Copy cell1C
 Else
     cell1B.Value = "N/A"
     cell1C.Value = "N/A"
 End If
Next

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.