Macro that finds value from two separate excels and combines them into one.

XcelNoobster

New Member
Joined
Jun 7, 2022
Messages
40
Given excel sheet "Report1" and report "Sheet1" from two seperate Excel file, how would I create a macro that:
1) inserts a new column called "ECR" to the left of Column A in "Report1"
2) loops through all of Column F of "Sheet1" and if the entry is present in Column B of "Report1"(Column A would be the empty ECR column), adds the value from Column A from "Sheet1" to that row in "Report1"
3) A new sheet would be created with the final entries in Report1

Sheet1:
Screenshot 2023-05-17 144154.png


Report1:
Screenshot 2023-05-17 144254.png


Final Result:
Screenshot 2023-05-17 144354.png
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi @XcelNoobster.
Thanks for posting on MrExcel.​

Given excel sheet "Report1" and report "Sheet1" from two seperate Excel file
According to the above we have 2 books.

Consider the following:
1. In a third workbook put the macro and save this third workbook as excel enable macros.​

2. In the macro I have named the 2 books as "Book 1.xlsx" and "Book 2.xlsx".​

3. In the macro you must put the real names of your books, according to the name of the sheets, in these lines of the macro:​
Rich (BB code):
  Set wb1 = Workbooks("book 1.xlsx")    'This workbook contains sheet "Report1"
  Set wb2 = Workbooks("book 2.xlsx")    'This workbook contains sheet "Sheet1"
4. Both books must be open.​
5. According to your third point:​
3) A new sheet would be created with the final entries in Report1
Then, in "Book 1" a new sheet will be created with the name "Final Result"​

6. You can change the name of the "Final Result" sheet in these lines of the macro:​
Rich (BB code):
  On Error Resume Next: wb1.Sheets("Final Result").Delete: On Error GoTo 0
  sh3.Name = "Final Result"

7. Put the macro in a module and make the changes mentioned above.​
VBA Code:
Sub finds_value()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim f As Range
  Dim i As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set wb1 = Workbooks("book 1.xlsx")    'This workbook contains sheet "Report1"
  Set wb2 = Workbooks("book 2.xlsx")    'This workbook contains sheet "Sheet1"
  Set sh1 = wb1.Sheets("Report1")
  Set sh2 = wb2.Sheets("Sheet1")
  
  sh1.Copy after:=wb1.Sheets(wb1.Sheets.Count)
  Set sh3 = wb1.Sheets(wb1.Sheets.Count)
  On Error Resume Next: wb1.Sheets("Final Result").Delete: On Error GoTo 0
  sh3.Name = "Final Result"
  
  sh3.Range("A:A").Insert
  sh3.Range("A1").Value = "ECR"
  For i = 2 To sh3.Range("B" & Rows.Count).End(3).Row
    Set f = sh2.Range("F:F").Find(sh3.Range("B" & i).Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      sh3.Range("A" & i).Value = sh2.Range("A" & f.Row).Value
    Else
      sh3.Range("A" & i).Value = "NONE"
    End If
  Next

  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub
--------------
Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.​
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.​

For example:
book 1.xlsx
ABC
1Item numberItem descLevel
244con1
366front2
434insu a3
533insu b4
Report1

book 2.xlsx
ABCDEF
1NumberDescStWorkflowChangeItem Number
2Test144
3Test255
4Test366
Sheet1


Final:
book 1.xlsx
ABCD
1ECRItem numberItem descLevel
2Test144con1
3Test366front2
4NONE34insu a3
5NONE33insu b4
Final Result



--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
Hi @XcelNoobster.
Thanks for posting on MrExcel.​


According to the above we have 2 books.

Consider the following:
1. In a third workbook put the macro and save this third workbook as excel enable macros.​

2. In the macro I have named the 2 books as "Book 1.xlsx" and "Book 2.xlsx".​

3. In the macro you must put the real names of your books, according to the name of the sheets, in these lines of the macro:​
Rich (BB code):
  Set wb1 = Workbooks("book 1.xlsx")    'This workbook contains sheet "Report1"
  Set wb2 = Workbooks("book 2.xlsx")    'This workbook contains sheet "Sheet1"
4. Both books must be open.​
5. According to your third point:​

Then, in "Book 1" a new sheet will be created with the name "Final Result"​

6. You can change the name of the "Final Result" sheet in these lines of the macro:​
Rich (BB code):
  On Error Resume Next: wb1.Sheets("Final Result").Delete: On Error GoTo 0
  sh3.Name = "Final Result"

7. Put the macro in a module and make the changes mentioned above.​
VBA Code:
Sub finds_value()
  Dim wb1 As Workbook, wb2 As Workbook
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim f As Range
  Dim i As Long
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Set wb1 = Workbooks("book 1.xlsx")    'This workbook contains sheet "Report1"
  Set wb2 = Workbooks("book 2.xlsx")    'This workbook contains sheet "Sheet1"
  Set sh1 = wb1.Sheets("Report1")
  Set sh2 = wb2.Sheets("Sheet1")
 
  sh1.Copy after:=wb1.Sheets(wb1.Sheets.Count)
  Set sh3 = wb1.Sheets(wb1.Sheets.Count)
  On Error Resume Next: wb1.Sheets("Final Result").Delete: On Error GoTo 0
  sh3.Name = "Final Result"
 
  sh3.Range("A:A").Insert
  sh3.Range("A1").Value = "ECR"
  For i = 2 To sh3.Range("B" & Rows.Count).End(3).Row
    Set f = sh2.Range("F:F").Find(sh3.Range("B" & i).Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      sh3.Range("A" & i).Value = sh2.Range("A" & f.Row).Value
    Else
      sh3.Range("A" & i).Value = "NONE"
    End If
  Next

  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub
--------------
Note XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.​
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.​

For example:
book 1.xlsx
ABC
1Item numberItem descLevel
244con1
366front2
434insu a3
533insu b4
Report1

book 2.xlsx
ABCDEF
1NumberDescStWorkflowChangeItem Number
2Test144
3Test255
4Test366
Sheet1


Final:
book 1.xlsx
ABCD
1ECRItem numberItem descLevel
2Test144con1
3Test366front2
4NONE34insu a3
5NONE33insu b4
Final Result



--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------

What would I have to change in your macro so it can read two separate open Worksheets(both reports are .xls) instead of Workbooks?
 
Upvote 0
Did you try what I gave you?

Or do you just change your mind and want a new answer?

What would I have to change in your macro so it can read two separate open Worksheets(both reports are .xls) instead of Workbooks?
Anyway, I don't understand your request.

Can you detail what you need?
The 2 sheets are in the same bookX?
Is bookX going to contain the macro?
In this same bookX, do you want the new sheet?
What is the name of bookX?
What are the sheets called?
What will the new sheet be called?

Without all the detailed information and without examples in the XL2BB tool I'm afraid I can't help you anymore.
 
Upvote 0
Did you try what I gave you?

Or do you just change your mind and want a new answer?


Anyway, I don't understand your request.

Can you detail what you need?
The 2 sheets are in the same bookX?
Is bookX going to contain the macro?
In this same bookX, do you want the new sheet?
What is the name of bookX?
What are the sheets called?
What will the new sheet be called?

Without all the detailed information and without examples in the XL2BB tool I'm afraid I can't help you anymore.
Your macro logic is correct, its just that the Excel files are not workbooks(.xlsx), but rather worksheets(.xls) extension. So instead of :
Set wb1 = Workbooks("book 1.xlsx") 'This workbook contains sheet "Report1"
Set wb2 = Workbooks("book 2.xlsx") 'This workbook contains sheet "Sheet1"

it should be:
Worksheet("Report1.xls") and Worksheet("Sheet1.xls") ----> The two separate excel file.

Maybe the names i used are a bit confusing but I have two seperate report with .xls extension that i need to work with.

so Report1.xls has sheet1 and Report2.xls alos has a sheet1.
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,187
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top