Hi all,
I built a set of macros that is supposed to consolidate deposit information from four workbooks into one worksheet, and then build an index from four additional workbooks to pair customer names with deposit data. Unfortunately, this macro takes over an hour to complete, and I am unsure of how to make it an more efficient (I am self taught and still learning!). Any pointers on how to make this code more efficient would be greatly appreciated. When it is all said and done, there are over 200,000 lines of data being manipulated --> which may be the main cause for lag. I will post the code below. Thank you in advance!
I built a set of macros that is supposed to consolidate deposit information from four workbooks into one worksheet, and then build an index from four additional workbooks to pair customer names with deposit data. Unfortunately, this macro takes over an hour to complete, and I am unsure of how to make it an more efficient (I am self taught and still learning!). Any pointers on how to make this code more efficient would be greatly appreciated. When it is all said and done, there are over 200,000 lines of data being manipulated --> which may be the main cause for lag. I will post the code below. Thank you in advance!
Code:
Sub GetDeposits()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call ClearData
Call TTCdata
Call BOCdata
Call MBdata
Call VISTdata
Call BOCIndex
Call TTCIndex
Call VISTIndex
Call MBIndex
Call CustomerName
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Worksheets("Deposits").Activate
Cells.EntireColumn.AutoFit
Application.CalculateFullRebuild
End Sub
Option Explicit
Sub TTCdata()
Dim LastRowRange As Long
Dim LastRowData As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAccts As Worksheet
Set wsAccts = ThisWorkbook.Worksheets("Deposits")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\TTC Dep.xlsx")
Workbooks("TTC Dep.xlsx").ActiveSheet.Range("A1:A2").EntireRow.Copy Workbooks("Deposits 360 for Loren.xlsm").Worksheets("Deposits").Range("A1:A2").EntireRow
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowData = wsAccts.Cells(wsAccts.Rows.Count, "A").End(xlUp).Row + 1
.Range("A3", "AB" & LastRowRange).Copy wsAccts.Range("A" & LastRowData)
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub BOCdata()
Dim LastRowRange As Long
Dim LastRowData As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAccts As Worksheet
Set wsAccts = ThisWorkbook.Worksheets("Deposits")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\BOC Dep.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowData = wsAccts.Cells(wsAccts.Rows.Count, "A").End(xlUp).Row + 1
.Range("A3", "AB" & LastRowRange).Copy wsAccts.Range("A" & LastRowData)
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub MBdata()
Dim LastRowRange As Long
Dim LastRowData As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAccts As Worksheet
Set wsAccts = ThisWorkbook.Worksheets("Deposits")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\MB Dep.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowData = wsAccts.Cells(wsAccts.Rows.Count, "A").End(xlUp).Row + 1
.Range("A3", "AB" & LastRowRange).Copy wsAccts.Range("A" & LastRowData)
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub VISTdata()
Dim LastRowRange As Long
Dim LastRowData As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsAccts As Worksheet
Set wsAccts = ThisWorkbook.Worksheets("Deposits")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\VIST Dep.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowData = wsAccts.Cells(wsAccts.Rows.Count, "A").End(xlUp).Row + 1
.Range("A3", "AB" & LastRowRange).Copy wsAccts.Range("A" & LastRowData)
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub BOCIndex()
Dim LastRowRange As Long
Dim LastRowIndex As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsIndex As Worksheet
Set wsIndex = ThisWorkbook.Worksheets("Index")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\BOC Cust.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowIndex = wsIndex.Cells(wsIndex.Rows.Count, "B").End(xlUp).Row + 1
.Range("A4", "D" & LastRowRange).Copy
wsIndex.Range("B" & LastRowIndex).PasteSpecial xlPasteValues
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub TTCIndex()
Dim LastRowRange As Long
Dim LastRowIndex As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsIndex As Worksheet
Set wsIndex = ThisWorkbook.Worksheets("Index")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\TTC Cust.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowIndex = wsIndex.Cells(wsIndex.Rows.Count, "B").End(xlUp).Row + 1
.Range("A4", "D" & LastRowRange).Copy
wsIndex.Range("B" & LastRowIndex).PasteSpecial xlPasteValues
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub VISTIndex()
Dim LastRowRange As Long
Dim LastRowIndex As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsIndex As Worksheet
Set wsIndex = ThisWorkbook.Worksheets("Index")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\VIST Cust.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowIndex = wsIndex.Cells(wsIndex.Rows.Count, "B").End(xlUp).Row + 1
.Range("A4", "D" & LastRowRange).Copy
wsIndex.Range("B" & LastRowIndex).PasteSpecial xlPasteValues
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub MBIndex()
Dim LastRowRange As Long
Dim LastRowIndex As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsIndex As Worksheet
Set wsIndex = ThisWorkbook.Worksheets("Index")
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "\MB Cust.xlsx")
For Each ws In wb.Worksheets
With ws
LastRowRange = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
LastRowIndex = wsIndex.Cells(wsIndex.Rows.Count, "B").End(xlUp).Row + 1
.Range("A4", "D" & LastRowRange).Copy
wsIndex.Range("B" & LastRowIndex).PasteSpecial xlPasteValues
End With
Next ws
wb.Close savechanges:=False
End Sub
Sub ClearData()
Windows("Deposits 360 for Loren.xlsm").Activate
Worksheets("Deposits").Range("A:AB").Clear
Worksheets("Index").Range("B:E").Clear
End Sub
Sub CustomerName()
Dim LastRowData As Long
LastRowData = Worksheets("Deposits").Range("A1").End(xlDown).Row
Dim OgNames As Long
OgNames = Worksheets("Deposits").Range("AC1").End(xlDown).Row
Worksheets("Deposits").Range("AC:AC").Clear
Worksheets("Deposits").Range("AC2").FormulaR1C1 = _
"=INDEX(Index!C[-25], MATCH(RC[-15], Index!C[-26], 0))"
Worksheets("Deposits").Range("AC2").AutoFill Destination:=Worksheets("Deposits").Range("AC2:AC" & LastRowData)
End Sub
Last edited by a moderator: