With the following code I open workbooks located in a specified directory, copy and paste add ranges from the open workbook to a single destination workbook.
For example, each workbook contains several sheet beginning with "New Init" and each sheets data should be added/summarized/merged into a worksheet by the same name in the destination workbook. Six workbooks containing the worksheet "New Init- Parts" copies several ranges and pastes them into a Consolidation workbook with New Init- Parts worksheet.
There are eight worksheets beginning with the same name and the same layout.
Unfortunately it takes several minutes to run because it's so poorly designed.
Any help would be appreciated.
For example, each workbook contains several sheet beginning with "New Init" and each sheets data should be added/summarized/merged into a worksheet by the same name in the destination workbook. Six workbooks containing the worksheet "New Init- Parts" copies several ranges and pastes them into a Consolidation workbook with New Init- Parts worksheet.
There are eight worksheets beginning with the same name and the same layout.
Unfortunately it takes several minutes to run because it's so poorly designed.
Any help would be appreciated.
Code:
Sub Consolidate()
'
Dim intRow As Integer
Dim strFile As String, strPath As String
Dim wsDest As Worksheet, wbSource As Workbook
Dim rngSalesRegions As Range, rngArea As Range
'
Form1.Hide
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.AskToUpdateLinks = False
'
Set wsDest = ActiveSheet
strPath = InputBox("Identify the path to consolidate.", "Consolidation Path", "s:\accounting\oesch\strategicplan\")
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
intRow = Range("STARTFILENAMES").Row
'
' Instruction filename list
Sheets("Instructions").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Range("filenames").ClearContents
Set rngSalesRegions = wsDest.Range("filenames")
Do Until strFile = ""
'
' List files included in consolidation
Cells(intRow, 2) = strFile
intRow = intRow + 1
strFile = Dir()
Loop
wsDest.Range("A1").Select
'
' Consolidate New Init- LCCS
'
Sheets("New Initiatives- LCCS").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
'
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Initiatives- LCCS").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate New Init- Other Cost
'
Sheets("New Init- Other Cost").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
'
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Other Cost").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate New Init- Geo Exp
'
Sheets("New Init- Geo Exp").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Geo Exp").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate New Init- Product Exp
'
Sheets("New Init- Product Exp").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Product Exp").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
'
' Consolidate New Init- Market Exp
'
Sheets("New Init- Market Exp").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Market Exp").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate New Init- Facility Conso
'
Sheets("New Init- Facility Conso").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Facility Conso").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate New Init- Parts
'
Sheets("New Init- Parts").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Parts").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate New Init- Other
'
Sheets("New Init- Other").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("E6:Q20,W19:AC19,W23:AC23,W29:AC29,W35:AC35,W41:AC41,W47:AC47")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("New Init- Other").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
'
' Consolidate Base
Sheets("Base LOB Plan").Select
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("D11:P12,D18:P19,D23:P23,D27:P28,D29:P29,D33:P33,D42:P44,W17:AC17,W21:AC21,W25:AC25,W31:AC31,W35:AC35,W42:AC42")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("Base LOB Plan").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
'
' Consolidate Total
Sheets("Total LOB Plan").Select
'
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("D11:P12,D18:P19,D23:P23,D27:P29,D33:P33,D42:P44,W17:AC17,W21:AC21,W25:AC25,W31:AC31,W35:AC35,W42:AC42")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("Total LOB Plan").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
wsDest.Range("A1").Select
wsDest.Protect
'
' Consolidate Economic Profit
'
Sheets("Economic Profit").Select
'
Set wsDest = ActiveSheet
strFile = Dir(strPath & "strategic*.xls")
wsDest.Unprotect
'
Set rngSalesRegions = wsDest.Range("D28:D31,D56")
rngSalesRegions.ClearContents
Do Until strFile = ""
Set wbSource = Workbooks.Open(strPath & strFile)
'
For Each rngArea In rngSalesRegions.Areas
' Consolidate for each sales region
wbSource.Sheets("Economic Profit").Range(rngArea.Address).Copy
rngArea.PasteSpecial Paste:=xlPasteValues, Operation:=xlAdd, _
SkipBlanks:=True, Transpose:=False
Next rngArea
wbSource.Close SaveChanges:=False
strFile = Dir()
Loop
'
wsDest.Range("A1").Select
wsDest.Protect
'
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
Calculate
'
MsgBox "Consolidation Complete"
End Sub