VBA code to combine two Workbooks into one Worksheet

vbanewbie68

Board Regular
Joined
Oct 16, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I want to know if it is possible to bring two workbooks (sheet1) into one worksheet (master workbook). Two data in one worksheet.

Any help is really appreciated. Below are screenshots for further details if helps.

Two files:

  • Fundraise-pages(1).csv
  • Supporters(1).csv
1656020428373.png


Two workbooks have different data. See the headers from each one.

Fundraise-pages(1).csv

Company NamePersonEmailTitleCreated DateUrlFundraiser Page IDEvent Page IDEvent Page TitleEvent Page URLCurrencyRaised (inc. Gift Aid)TargetStatusDonors custom code 1Donors custom code 2Donors custom code 3Donors custom code 4Cons IDAppeal_Package IDFund IDTribute IDEvents custom code 1Events custom code 2Events custom code 3Events custom code 4Team page custom code 1Team page custom code 2Team page custom code 3Team page custom code 4

1656020521261.png


1656020532884.png


Supporters(1).csv

Supporter IDTitleFirst NameLast NameEmailAddressTownPostcodeCountryPhoneJoin DateCurrencyLifetime DonationsLifetime Gift AidLast Donation DateDonors custom code 1Donors custom code 2Donors custom code 3Donors custom code 4Marketing Opt-in AnswerMarketing Opt-in DateEmail Opt-inPhone Opt-inSMS Opt-inPost Opt-in
 

Attachments

  • 1656020643616.png
    1656020643616.png
    200.5 KB · Views: 5

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
VBA Code:
Sub Test()

Dim wb As Workbook
Dim wb2 As Workbook
Dim x As Long
Dim y As Long
Dim x2 As Long
Dim y2 As Long

Set wb = Workbooks.Open("C:\Users\UserName\Downloads\File1.csv")

x = Cells(Rows.Count, 1).End(xlUp).Row + 1
y = Cells(1, Columns.Count).End(xlToLeft).Column

Set wb2 = Workbooks.Open("C:\Users\UserName\Downloads\File2.csv")

x2 = Cells(Rows.Count, 1).End(xlUp).Row
y2 = Cells(1, Columns.Count).End(xlToLeft).Column

wb.Sheets(1).Range(Cells(2, 1).Address, Cells(x2, y2).Address).Copy Destination:=wb2.Sheets(1).Range(Cells(x, 1).Address, Cells(x, 1).Address)

End Sub
 
Upvote 0
Hi Coding4Fun

Thank you so much for your time on this.

Please see below the link at the bottom as I use XL2BB. This is my first time I use XL2BB. Do let me know if this is not working?

With your codes, it's looking great when I run a test, but just a small thing is that I would like to move the data from the bottom to move up onto the right hand side instead as see the examples of screenshots including headers. I hope the screenshots are helpful.

Also File 1 data and file 2 data- both have the same email addresses. Apologies for not mentioning it in my initial question.

Importantly I would like a macro to pull data to identify or even match for both email addresses. If found then add it onto a worksheet. Does this make sense?

I hope everything is clear.

I looking forward to hearing from you asap.

Best regards

V



1656067922814.png



1656067935693.png





XL2BB.

example.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBC
1Supporter IDTitleFirst NameLast NameEmailAddressTownPostcodeCountryPhoneJoin DateCurrencyLifetime DonationsLifetime Gift AidLast Donation DateDonors custom code 1Donors custom code 2Donors custom code 3Donors custom code 4Marketing Opt-in AnswerMarketing Opt-in DateEmail Opt-inPhone Opt-inSMS Opt-inPost Opt-inCompany NamePersonEmailTitleCreated DateUrlFundraiser Page IDEvent Page IDEvent Page TitleEvent Page URLCurrencyRaised (inc. Gift Aid)TargetStatusDonors custom code 1Donors custom code 2Donors custom code 3Donors custom code 4Cons IDAppeal_Package IDFund IDTribute IDEvents custom code 1Events custom code 2Events custom code 3Events custom code 4Team page custom code 1Team page custom code 2Team page custom code 3Team page custom code 4
2Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
3Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
4Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
5Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
6Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
7Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
8Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
9Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
10Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
11Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
12Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 1Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2Existing Data 2
13
14
Sheet1
 
Upvote 0
VBA Code:
Sub Test()

Dim wb As Workbook
Dim wb2 As Workbook
Dim x As Long
Dim y As Long
Dim x2 As Long
Dim y2 As Long

Set wb = Workbooks.Open("C:\Users\UserName\Downloads\File1.csv")

x = Cells(Rows.Count, 1).End(xlUp).Row
y = Cells(1, Columns.Count).End(xlToLeft).Column + 1

Set wb2 = Workbooks.Open("C:\Users\UserName\Downloads\File2.csv")

x2 = Cells(Rows.Count, 1).End(xlUp).Row
y2 = Cells(1, Columns.Count).End(xlToLeft).Column

wb.Sheets(1).Range(Cells(1, 1).Address, Cells(x2, y2).Address).Copy Destination:=wb2.Sheets(1).Range(Cells(1, y).Address, Cells(1, y).Address)

End Sub
 
Upvote 0
Solution
VBA Code:
Sub Test()

Dim wb As Workbook
Dim wb2 As Workbook
Dim x As Long
Dim y As Long
Dim x2 As Long
Dim y2 As Long

Set wb = Workbooks.Open("C:\Users\UserName\Downloads\File1.csv")

x = Cells(Rows.Count, 1).End(xlUp).Row
y = Cells(1, Columns.Count).End(xlToLeft).Column + 1

Set wb2 = Workbooks.Open("C:\Users\UserName\Downloads\File2.csv")

x2 = Cells(Rows.Count, 1).End(xlUp).Row
y2 = Cells(1, Columns.Count).End(xlToLeft).Column

wb.Sheets(1).Range(Cells(1, 1).Address, Cells(x2, y2).Address).Copy Destination:=wb2.Sheets(1).Range(Cells(1, y).Address, Cells(1, y).Address)

End Sub

That is much better now and thank you for that. :) I do really appreciate your time on this matter.

I deleted the blank columns because in the middle section in the worksheet it was blank. I use this code Columns("Z:AD").Delete or do you have a better suggestion for that? Please see the codes below.

I would like For file 1 (email address) to match File 2 (email address). If both matches are found then I want them to move to a worksheet (master). I have mentioned this on my previous comment.

Sub Test()

Dim wb As Workbook
Dim wb2 As Workbook
Dim x As Long
Dim y As Long
Dim x2 As Long
Dim y2 As Long
Dim sh As Worksheet

Set wb = Workbooks.Open("J:\PS\FSD_Rest\SOPS_Data\Database Team\fundraise-pages (1).csv")

x = Cells(Rows.Count, 1).End(xlUp).Row
y = Cells(1, Columns.Count).End(xlToLeft).Column + 1

Set wb2 = Workbooks.Open("J:\PS\FSD_Rest\SOPS_Data\Database Team\MacrosSupporters (1).csv")

x2 = Cells(Rows.Count, 1).End(xlUp).Row
y2 = Cells(1, Columns.Count).End(xlToLeft).Column


wb.Sheets(1).Range(Cells(1, 1).Address, Cells(x2, y2).Address).Copy Destination:=wb2.Sheets(1).Range(Cells(1, y).Address, Cells(1, y).Address)

'Delete columns range "Z to AD"
Columns("Z:AD").Delete

' Match function to match or vlookup from file 1 and 2 email address and if found then add it to a worksheet.


End Sub




Thank you

Best regards

V
 
Upvote 0
That is much better now and thank you for that. :) I do really appreciate your time on this matter.

I deleted the blank columns because in the middle section in the worksheet it was blank. I use this code Columns("Z:AD").Delete or do you have a better suggestion for that? Please see the codes below.

I would like For file 1 (email address) to match File 2 (email address). If both matches are found then I want them to move to a worksheet (master). I have mentioned this on my previous comment.

Sub Test()

Dim wb As Workbook
Dim wb2 As Workbook
Dim x As Long
Dim y As Long
Dim x2 As Long
Dim y2 As Long
Dim sh As Worksheet

Set wb = Workbooks.Open("J:\PS\FSD_Rest\SOPS_Data\Database Team\fundraise-pages (1).csv")

x = Cells(Rows.Count, 1).End(xlUp).Row
y = Cells(1, Columns.Count).End(xlToLeft).Column + 1

Set wb2 = Workbooks.Open("J:\PS\FSD_Rest\SOPS_Data\Database Team\MacrosSupporters (1).csv")

x2 = Cells(Rows.Count, 1).End(xlUp).Row
y2 = Cells(1, Columns.Count).End(xlToLeft).Column


wb.Sheets(1).Range(Cells(1, 1).Address, Cells(x2, y2).Address).Copy Destination:=wb2.Sheets(1).Range(Cells(1, y).Address, Cells(1, y).Address)

'Delete columns range "Z to AD"
Columns("Z:AD").Delete

' Match function to match or vlookup from file 1 and 2 email address and if found then add it to a worksheet.


End Sub




Thank you

Best regards

V
Hi could you let me know if that is possible to either to use Vlookup or match function to rely on both emails?
Thanks

Regards
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,165
Members
452,615
Latest member
bogeys2birdies

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