Hi Team,
Below is the macro I am using for colating data from all excel files,
I have 250 excel files in a Single Folder which I want to collate, This Macro works fast till 50 files,
Then Macro slows down,
is there is any other way to increase speed, using collection/Array/Recordset,
or Can you help me in existing code to increase speed. Thanks in advance!
Thanks
mg
Below is the macro I am using for colating data from all excel files,
I have 250 excel files in a Single Folder which I want to collate, This Macro works fast till 50 files,
Then Macro slows down,
is there is any other way to increase speed, using collection/Array/Recordset,
or Can you help me in existing code to increase speed. Thanks in advance!
VBA Code:
Option Explicit
Sub Consolidate_All_workbook()
Dim fso As New FileSystemObject
Dim mainfold As Scripting.Folder
Dim subfold As Scripting.Folder
Dim myfile As Scripting.file
Dim firstfile As String
Dim Filename As String
Dim ws As Worksheet
Dim cnt As Integer
Dim wb As Workbook
Dim nwbk As Workbook
Set nwbk = Workbooks.Add
Dim nlr As Long
Dim strSearch As String
Dim lr As Long
Dim lc As Long
Dim t As Single
t = Timer
Dim repeat_sht As String
repeat_sht = Mac.Range("b6").Value
Dim countfile As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
On Error GoTo eh:
Set mainfold = fso.GetFolder(Mac.Range("b3").Value)
For Each myfile In mainfold.Files
cnt = cnt + 1
Set wb = Workbooks.Open(myfile.Path, UpdateLinks:=False, ReadOnly:=True)
Set ws = wb.Worksheets(repeat_sht) '
' If ws.AutoFilterMode = True Then ws.AutoFilterMode = False
' If ws.FilterMode = True Then ws.ShowAllData
countfile = countfile + 1
If cnt = 1 Then
lc = ws.UsedRange.Columns.Count
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Range("a7").Resize(lr, lc).Copy '
nwbk.Worksheets(1).Range("B1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
nwbk.Worksheets(1).Range("a1").Value = "Fund Name"
nwbk.Worksheets(1).Range("a2").Resize(lr - 7).Value = ws.Range("b3")
wb.Close False
Else
nlr = nwbk.Worksheets(1).Range("b1").CurrentRegion.Rows.Count + 1
lc = ws.UsedRange.Columns.Count
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Range("a8").Resize(lr, lc).Copy
nwbk.Worksheets(1).Range("B" & nlr).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
nwbk.Worksheets(1).Cells(nlr, 1).Resize(lr - 7).Value = ws.Range("b3")
wb.Close False
End If
countfile = countfile + 1
Next myfile
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
nwbk.Activate
ActiveSheet.Range("a1").Select
Application.CutCopyMode = False
MsgBox "Macro Successful Total " & countfile & " Files Consolidated in " & Timer - t & " .Seconds"
Exit Sub
eh:
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
MsgBox "Macro got stuck here for workbook " & wb.Name, vbInformation
End Sub
Thanks
mg