Retrieve a column of the same name from another workbook

VBAnewbie82

New Member
Joined
Nov 13, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have two large workbooks that I need to combine as one. First report is the budget for specific year (workbook BUDGET with one sheet “Sheet1”) and second is estimate for same year (workbook ESTIMATE with one sheet “Sheet1”). Layout of these are identical as follows, only the numbers or the money in budget columns change:

Row1 = cost center numbers for each column
Row2 = cost center names
Row3 = header “Budget” for budget or estimate or header “Results” for current results
Row 4 to x = actual budgets and results in currency

Example for workbook “BUDGET” Sheet1 could look like this:
5555; 5555; 6666; 6666; 7777; 7777;
MAN; MAN; ICT; ICT; HRM; HRM;
Budget; Result; Budget; Result; Budget; Result;
900; 600; 700; 200; 500; 400;

Example for workbook “ESTIMATE” Sheet1 could look like this:
5555; 5555; 6666; 6666; 7777; 7777;
MAN; MAN; ICT; ICT; HRM; HRM;
Budget; Result; Budget; Result; Budget; Result;
1200; 600; 400; 200; 800; 400;

Example for what I am trying to achieve should look like this:
5555; 5555; 5555, 6666, 6666; 6666; 7777, 7777; 7777;
MAN; MAN; MAN; ICT; ICT; ICT; HRM; HRM; HRM;
Budget; Estimate; Result; Budget; Estimate; Result; Budget; Estimate; Result;
900; 1200; 600; 700; 400; 200; 500; 800; 400;

Macro could make this for a new workbook or use ESTIMATE workbook as the one where it is bringing columns from BUDGET. There are around 90 different cost centers = aroung 180 columns in each workbook and around 600 rows in both of them.
 

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.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of both sheets. Include 8 or 10 rows of data on each sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Here is the budget:
BUDGET.xlsx
ABCDEFG
1CCid555555556666666677777777
2CCnameMANMANICTICTHRMHRM
3BudgetResultBudgetResultBudgetResult
4Acc1900600700200500400
5Acc2900600700200500400
6Acc3900600700200500400
7Acc4900600700200500400
8Acc5900600700200500400
9Acc6900600700200500400
10Acc7900600700200500400
Sheet1

Here is the estimate:
ESTIMATE.xlsx
ABCDEFG
1CCid555555556666666677777777
2CCnameMANMANICTICTHRMHRM
3BudgetResultBudgetResultBudgetResult
4Acc11200600400200800400
5Acc21200600400200800400
6Acc31200600400200800400
7Acc41200600400200800400
8Acc51200600400200800400
9Acc61200600400200800400
10Acc71200600400200800400
Sheet1

Here is the final result that should be done with vba:
FINAL.xlsx
ABCDEFGHIJ
1CCid555555555555666666666666777777777777
2CCnameMANMANMANICTICTICTHRMHRMHRM
3BudgetEstimateResultBudgetEstimateResultBudgetEstimateResult
4Acc19001200600700400200500800400
5Acc29001200600700400200500800400
6Acc39001200600700400200500800400
7Acc49001200600700400200500800400
8Acc59001200600700400200500800400
9Acc69001200600700400200500800400
10Acc79001200600700400200500800400
Sheet1
 
Upvote 0
Start with both workbooks open. Copy/paste this macro into a regular module in the Budget workbook. Create a sheet named "Estimate" in the Estimate workbook. Change the workbook name (in red) to suit your needs. Run the macro.
Rich (BB code):
Sub GetData()
    Application.ScreenUpdating = False
    Dim lRow As Long, desWB As Workbook, srcWS1 As Worksheet, srcWS2 As Worksheet, desWS As Worksheet, x As Long, lCol As Long, y As Long: y = 2
    Set desWB = Workbooks("Estimate.xlsx")
    Set srcWS1 = ThisWorkbook.Sheets("Sheet1")
    Set srcWS2 = desWB.Sheets("Sheet1")
    Set desWS = desWB.Sheets("Estimate")
    lRow = srcWS1.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lCol = srcWS1.Cells(1, Columns.Count).End(xlToLeft).Column
    desWS.Cells(1, 1).Resize(lRow).Value = srcWS1.Cells(1, 1).Resize(lRow).Value
    For x = 2 To lCol Step 2
        With desWS
            .Cells(1, y).Resize(lRow).Value = srcWS1.Cells(1, x).Resize(lRow).Value
            .Cells(1, y + 1).Resize(lRow).Value = srcWS2.Cells(1, x).Resize(lRow).Value
            .Cells(3, y + 1) = "Estimate"
            .Cells(1, y + 2).Resize(lRow).Value = srcWS1.Cells(1, x + 1).Resize(lRow).Value
        End With
        y = y + 3
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you @mumps it works!
Is there a way of saving this macro in personal macro workbook instead of using copy/paste into a regular module in Budget? I tried it saving, but then it gives "Run-time error '91': Object variable or With block variable not set" and the row it highlights is:
lRow = srcWS1.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
Upvote 0
I don't use a personal macro workbook. However, I believe that the problem is caused by the variable 'srcWS1' referring to Sheet1 of the macro workbook instead of the Budget workbook. You could try to change the reference in this line of code:
VBA Code:
Set srcWS1 = ThisWorkbook.Sheets("Sheet1")
to
Rich (BB code):
Set srcWS1 =Workbooks("Budget.xlsx").Sheets("Sheet1")
Change the workbook name to suit your needs.
 
Upvote 0
VBA Code:
Set srcWS1 = ThisWorkbook.Sheets("Sheet1")
to
VBA Code:
Set srcWS1 = ActiveWorkbook.Sheets("Sheet1")

That seems to work best. Then I don't have to mind the workbook name where I'm running the macro.
 
Upvote 0
I just noticed that if Budget or Estimate doesn't have identical amount of rows, it doesn't work right. So let's tweek this a bit.

If Budget.xlsx Sheet1 contains only odd account numbers (1, 3, 5 and 7) like this:
Budget.xlsx
ABCDEFG
1CCid555555556666666677777777
2CCnameMANMANICTICTHRMHRM
3BudgetResultBudgetResultBudgetResult
4Acc1900600700200500400
5Acc3900600700200500400
6Acc5900600700200500400
7Acc7900600700200500400
Sheet1

and Estimate.xlsx Sheet1 contains:
Estimate.xlsx
ABCDEFG
1CCid555555556666666677777777
2CCnameMANMANICTICTHRMHRM
3BudgetResultBudgetResultBudgetResult
4Acc11200600400200800400
5Acc21200600400200800400
6Acc31200600400200800400
7Acc41200600400200800400
8Acc51200600400200800400
9Acc61200600400200800400
10Acc71200600400200800400
Sheet1

the macro's final results is as follows, so it is missing some accounts:
Estimate.xlsx
ABCDEFGHIJ
1CCid555555555555666666666666777777777777
2CCnameMANMANMANICTICTICTHRMHRMHRM
3BudgetEstimateResultBudgetEstimateResultBudgetEstimateResult
4Acc19001200600700400200500800400
5Acc39001200600700400200500800400
6Acc59001200600700400200500800400
7Acc79001200600700400200500800400
Estimate



...so let't try it this way. Next "screenshots" layouts are as close to real as possible. For each account there is account number in column A and its name in column B. In real life there might be a situation where there is results without budget or vice versa. I put data (money) in 3 digit format to help with error checking. First number is same as cost centers number, seconds matches account number and third is 1 for budget, 2 for estimate and 3 for result.

Original budget would be like this, missing account 4, and some budgets without result or results without budget:
Budget.xlsx
ABCDEFGHIJ
1CCid55555555666666667777777788888888
2CCnameMANMANICTICTHRMHRMETCETC
3BudgetResultBudgetResultBudgetResultBudgetResult
4Acc1Account name1511611613711713811813
5Acc2Account name2521523621623721723821823
6Acc3Account name3531533631633731733831833
7Acc5Account name5551553651653751753851853
8Acc6Account name6563863
9Acc7Account name7571573671673771773871873
Sheet1


Estimate might look like this with again some estimates (budget -header) without results. Notice that there is now account 4.
Estimate.xlsx
ABCDEFGHIJ
1CCid55555555666666667777777788888888
2CCnameMANMANICTICTHRMHRMETCETC
3BudgetResultBudgetResultBudgetResultBudgetResult
4Acc1Account name1512612613712713812813
5Acc2Account name2522523622623722723822823
6Acc3Account name3532533632633732733832833
7Acc4Account name4542642643742
8Acc5Account name5552553652653752753852853
9Acc6Account name6562662762862863
10Acc7Account name7572573672673772773872873
Sheet1


And when these two are combined, the end should look like this:
Estimate.xlsx
ABCDEFGHIJKLMN
1CCid555555555555666666666666777777777777888888888888
2CCnameMANMANMANICTICTICTHRMHRMHRMETCETCETC
3BudgetEstimateResultBudgetEstimateResultBudgetEstimateResultBudgetEstimateResult
4Acc1Account name1511512611612613711712713811812813
5Acc2Account name2521522523621622623721722723821822823
6Acc3Account name3531532533631632633731732733831832833
7Acc4Account name4542642742
8Acc5Account name5551552553651652653751752753851852853
9Acc6Account name6562563662762862863
10Acc7Account name7571572573671672673771772773871872873
Estimate
 
Upvote 0
I assume that the following scenarios are also possible:
-Either the Budget sheet or the Estimate sheet could have the larger number of rows
-The order of the CCnames in column A is not necessarily the same in each of the two sheets
-There could be a CCname in Budget that is not included in Estimate and vice versa
Are all of these scenarios possible?
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,225
Members
448,877
Latest member
gb24

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