Consolidate Multiple Worksheets into one Worksheet

JohanGduToit

Board Regular
Joined
Nov 12, 2021
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Greetings Forum,

I receive Excel Files containing customer sales information on a weekly basis which I have to import into MS Access. I have to reformat them prior to importing. Each file consists of four worksheets and each worksheet contain the same number of columns (A to X) (each worksheet has various row counts).

I need to consolidate the four worksheets into one 'Consolidated' worksheet within the same Excel file.

Before consolidating the four worksheets, the following must happen on each of these sheets:

(a) Rows 1 to 3 must be deleted
(b) Columns on each of the worksheets run from "A" to "X"; but I am only interested in consolidating (copying) columns A, B, C, D, H and N (all the other columns may be ignored).

Once consolidated, the four original worksheets should be deleted so that only the 'Consolidated' worksheet remain (without any column headers).

The VBA procedure will reside within my MS Access application and will be called using the command : Call FormatMrPSport(fName) where fName will be the name of the Excel File to be processed.

Any help to assist me with some sample code would be greatly appreciated. My current code is designed to use late binding and what I have successfully deletes top 3 rows and deletion of columns; but failing on the consolidation part.

Many thanks!!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
SalesFile.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1T/Y Open SOHT/Y SalesT/Y Ave Week SalesT/Y Close SOHT/Y SITT/YT/Y Open SOHT/Y SalesT/Y Ave Week SalesS to ST/Y Close SOHT/Y SITCoverStockSalesT/Y PMOT/Y GPT/Y GP
2 Brc Branch DescriptionItem NumberItem DescriptionEnd UseUnitsUnitsUnitsUnitsUnitsClearance %ValueValueValueRatioValueValueContCont %Value %
3227061713171322318507.134183392.94296762.86296762.8614.14142246.8211629.513.0310010048.16119999.8346.5
4100Mr Price Sport Online6701010271Mxd Mns Digital LED watch blk20200259.9800259.98000.010000
5100Mr Price Sport Online6701010306Mens square Digi watch black101009.99009.990000000
6100Mr Price Sport Online6701010395Mxd Mns Digi Round Blk Khaki1010059.990059.990000000
7100Mr Price Sport Online6701010406Mxd Pedometer 2tone Mint 21858-10-100-49.9900-49.990000000
8100Mr Price Sport Online6701010427Multi PS-W-MX-23435 Khaki2020019.980019.980000000
9101BOKSBURG EAST RAND RETAIL PARK6701010271Mxd Mns Digital LED watch blk60600779.9400779.94000.020000
10101BOKSBURG EAST RAND RETAIL PARK6701010392Mxd LED Square Slim Look Pink1010059.990059.990000000
11101BOKSBURG EAST RAND RETAIL PARK6701010472Mxd MNS Digi Rnd CAMO SS1930300539.9700539.97000.010000
12101BOKSBURG EAST RAND RETAIL PARK6701010503Mns Digi Blk Blue 92630 AW206115016.671079.94179.99179.996899.95050.020.0648.2575.4848.23
13101BOKSBURG EAST RAND RETAIL PARK6701010511Mxd Mns Digital LED watch blk30300389.9700389.97000.010000
14101BOKSBURG EAST RAND RETAIL PARK6701010517Mxd BUBBLE Pedometer BLK40400719.9600719.96000.020000
15101BOKSBURG EAST RAND RETAIL PARK6701010518Mxd MNS Digi Rnd BLK ORAN S2040400719.9600719.96000.020000
16101BOKSBURG EAST RAND RETAIL PARK6701010524Mxd Mns Multi F BLK BLUE S2010100249.9900249.99000.010000
17101BOKSBURG EAST RAND RETAIL PARK6701010549MXD KIDS LION SS21 ANA21110010099.9999.9999.99100000.0348.2541.7448
18101BOKSBURG EAST RAND RETAIL PARK6701010552MXD KIDS UNICOR SLIM SS21 LED2-10-100-99.9900-99.990000000
19101BOKSBURG EAST RAND RETAIL PARK6701010563MXD PEDOM SAGE SS21 PED28117012.51199.92149.99149.9981049.93070.030.0547.8662.6148
20101BOKSBURG EAST RAND RETAIL PARK6701010564MXD PEDOM MINT SS21 PED3000000000000
21101BOKSBURG EAST RAND RETAIL PARK6701010565MXD PEDOM PNK SS21 PED360600899.9400899.94000.020000
22101BOKSBURG EAST RAND RETAIL PARK6701010567MXD BUBBLE TURQ SS21 PED2606001079.94001079.94000.030000
23101BOKSBURG EAST RAND RETAIL PARK6701010569MXD BUBBLE PINK SS21 PED320200359.9800359.98000.010000
24101BOKSBURG EAST RAND RETAIL PARK6701010572MXD LDS MINT RND SS21 DIGI 27225028.571259.93359.98359.983.5899.9502.50.020.1247.93150.2448
25101BOKSBURG EAST RAND RETAIL PARK6701010573MXD LDS WHITE RND SS21 DIGI 3000000000000
26101BOKSBURG EAST RAND RETAIL PARK6701010575MXD LDS GREEN RND SS21 DIGI 4000000000000
27101BOKSBURG EAST RAND RETAIL PARK6701010576MXD LDS PINK REC SS21 DIGI 511100100179.99179.99179.99100000.0648.2575.1248
28101BOKSBURG EAST RAND RETAIL PARK6701010578MXD LDS GREY RND S SS21 DIGI 610100179.9900179.990000000
29101BOKSBURG EAST RAND RETAIL PARK6701010591MXD MNS RND BLK SS21 MULTI F 230300749.9700749.97000.020000
30101BOKSBURG EAST RAND RETAIL PARK6701010592MXD MNS REC OLI SS21 MULTI F 310100249.9900249.99000.010000
31101BOKSBURG EAST RAND RETAIL PARK6701010593MXD MNS RND GREY SS21 MULTI F410100249.9900249.99000.010000
32101BOKSBURG EAST RAND RETAIL PARK6701010596MXD LDS REC ROSE SS21 MULTI F2505001399.95001399.95000.030000
33101BOKSBURG EAST RAND RETAIL PARK6701010597MXD MNS RND CAMO SS21 MULTI F120200559.9800559.98000.010000
34101BOKSBURG EAST RAND RETAIL PARK6701010598MXD MNS RND CAMO SS21 MULTI F211100100279.99279.99279.99100000.0947.84116.8748
35101BOKSBURG EAST RAND RETAIL PARK6701010599MXD RND ARRW BLK SS21 MULTI F1101190103499.9349.99349.99103149.91090.080.1248.08146.0848
36101BOKSBURG EAST RAND RETAIL PARK6701010600MXD RND NAVY SS21 MULTI F2303001049.97001049.97000.030000
37101BOKSBURG EAST RAND RETAIL PARK6701010601MXD REC BLUE SS21 MULTI F PED220200759.9800759.98000.020000
38101BOKSBURG EAST RAND RETAIL PARK6701010603MXD RND CAMO SS21 ANA DIGI2111050999.98499.99499.992499.99010.010.1748.02208.6948
39101BOKSBURG EAST RAND RETAIL PARK6701010606MXD KIDS COMBO- BLUES20200319.9800319.98000.010000
40101BOKSBURG EAST RAND RETAIL PARK6701010609MXD KIDS LED 1700700699.93000699.93000.020000
41101BOKSBURG EAST RAND RETAIL PARK6701010610MXD KIDS LED 211011001099.89001099.89000.030000
42101BOKSBURG EAST RAND RETAIL PARK6701010612MXD KIDS LED 3173314017.651699.83299.97299.975.671399.8604.670.030.147.86125.2248
43101BOKSBURG EAST RAND RETAIL PARK6701010614MXD DIGITAL LED 3 PINK50500649.9500649.95000.020000
FT Watches
 
Upvote 0
There are 4 worksheets : "FT Watches', 'FT Sunglasses', 'OD Sunglasses' and 'Sunglass Cases' that must be consolidated into one 'Consolidated' worksheet within the same Excel file. (Without the top 3 rows and only columns A, B, C. D, H and N are applicable).
 
Upvote 0
Here is what I have so far... the Insert of the 'Consolidated' worksheet works; but it creates additional Worksheets 2, 3 and 4 as well. I would like the new worksheet to be created as the 1st worksheet. (the commented code does not want to work).

Struggling to copy/consolidate the 4x worksheet's data to/on the 'Consolidated' worksheet.

Please advise...

VBA Code:
[CODE=vba]Public Sub FormatMrPSportTEST02(sFile As String)

'On Error GoTo Err_FormatMrPSport
    
    Dim xlApp As Object
    Dim xlSheet As Object
    
    Application.SetOption "Show Status Bar", True
    vStatusBar = SysCmd(acSysCmdSetStatus, "Formatting Mr P Sports File... Please wait.")

    Set xlApp = CreateObject("Excel.Application")
    Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets(1)

    With xlApp
        .Application.ScreenUpdating = False
        .Application.DisplayAlerts = False
        
        .Application.Sheets(Array("FT Watches", "FT Sunglasses", "OD Sunglasses", "Sunglass Cases")).Select
        .Application.Rows("1:3").Select
        .Application.Selection.Delete shift:=xlUp
        .Application.Range("E:E,F:F,G:G,I:I,J:J,K:K,L:L,M:M,O:O,P:P,Q:Q,R:R,S:S,T:T,U:U,V:V,W:W,X:X").Select
        .Application.Selection.Delete shift:=xlToLeft
        .Application.Sheets.Add.Name = "CONSOLIDATED"
        '.Application.Sheets.Add(Before:=Sheets("FT Watches")).Name = "CONSOLIDATED"   'THIS LINE AND THE ONE BELOW DOES NOT WORK
        '.Application.Sheets.Add(Before:=Sheets(1)).Name = "CONSOLIDATED"       
        
        '.Application.Sheets("FT Watches").Select     'UNABLE TO SELECT ALL CELLS CONTAINING DATA, THE LINE CONTAINING "xlLastCell " FAILS
        '.Application.Range("A1").Select
        '.Application.Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
        '.Application.Selection.Copy
        '.Application.Sheets("CONSOLIDATED").Select
        '.Application.Range("A1").Select
        '.Application.ActiveSheet.Paste
                
        .Application.Range("A1").Select
        .Application.ActiveWorkbook.Save
        .Application.ActiveWorkbook.Close
        .Application.DisplayAlerts = True
        .Quit
   
        .Application.ScreenUpdating = False
        .Application.DisplayAlerts = False
   End With
   
   vStatusBar = SysCmd(acSysCmdClearStatus)

   Set xlSheet = Nothing
   Set xlApp = Nothing

    
'Exit_FormatMrPSportTEST:
'    Exit Sub
    
'Err_FormatMrPSportTEST:
'    vStatusBar = SysCmd(acSysCmdClearStatus)
'    MsgBox Err.Number & " - " & Err.Description
'    Set xlSheet = Nothing
'    Set xlApp = Nothing
'    Resume Exit_FormatMrPSportTEST
    
End Sub
[/CODE]
 
Upvote 0
I'm sure there must be an easier way to do this; maybe looping through the worksheets (Sheet(1) to CountSheets) and append each sheet's data (Range A1 to LastRow/Columns A to F) to the 'Consolidated' worksheet? Just don't know how to code that...
 
Upvote 0
Quite frankly, I would not approach it that way.
I would create the Macros/VBA to manipulate the Excel data right in Excel (I find that much easier than trying to do it through Access VBA).
Then I would simply call that Excel VBA code from Access VBA.
 
Upvote 0
Hi Joe,

Unfortunately I have to do if from Access... we receive a new file from the customer once a week and I am trying to automate the import process. I can not expect the user to copy and paste a macro into the new sheet every time. But thank you for your feedback...appreciated!
 
Upvote 0
Hi Joe,

Unfortunately I have to do if from Access... we receive a new file from the customer once a week and I am trying to automate the import process. I can not expect the user to copy and paste a macro into the new sheet every time. But thank you for your feedback...appreciated!
You don't have to. You can create a separate Excel file that has those macros and call that from your VBA code.
If you are trying to run them from Access, they are already running from code that is not in the actual data files.
This would just be storing them in Excel instead of Access.

The advantage to developing them in Excel is that you have the benfit of all the debugging tools, and it is a lot easier to develop and test.
 
Upvote 0
See if this site helps you.
You're going to have to loop over the sheets in question and copy/paste where required. I don't understand the comment about having debugging tools in Excel. The vb editor is the same, regardless whether you have Access open or Excel. As long as you have set the correct references, you can even run Excel-specific functions from Access. Can't say if there is a limited availability or not, just that I've done that but can't recall what functions I used.
 
Upvote 0
Solution
See if this site helps you.
You're going to have to loop over the sheets in question and copy/paste where required. I don't understand the comment about having debugging tools in Excel. The vb editor is the same, regardless whether you have Access open or Excel. As long as you have set the correct references, you can even run Excel-specific functions from Access. Can't say if there is a limited availability or not, just that I've done that but can't recall what functions I used.
Can you step through the VBA code line-by-line and watch what happens on the Excel side while it is happening?
(Maybe you can, I have never tried it)?

You certainly cannot take advantage of Excel's Macro Recorder using Access, though.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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