Loop Through Formula/Macro

ryansm05

Board Regular
Joined
Sep 14, 2016
Messages
148
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need a formula or macro that can loop through a particular file and return specified cells from EVERY file saved in this location (up to say 2,000 potential files). Furthermore, I'll need this formula / macro to sore the data alphabetically by client.

For a little more context, I'm needing to create a summary sheet for 100s of jobs that will be saved in a specific file location by project managers.

If anyone could help, I would be extremely grateful and in total awe.

Thanks
Ryan
 
Happy New Years Ryan! U seemed satisfied with the duration this code was taking to process files... I'm not... 28 minutes...yuck! Anyways, I suspect that it will be a lot quicker just to scrap all that copy and pasting and go with an array based solution. So, here's some new code for U to test. It looks like it should work but I'm unable to test it in your real world. HTH. Dave
Code:
Option Explicit

Sub test()
Dim LastRow As Double, sht As Worksheet, FSO As Object
Dim FlDr As Object, FileNm As Object, Cnt As Integer, Cnt2 As Integer, Cnt3 As Integer, Cnt4 As Integer
Dim RngArr() As Variant, RngArr2() As Variant, Rng As Range, Rng2 As Range
 On Error GoTo Erfix
 Application.Cursor = xlWait
 ThisWorkbook.Sheets("TAB1").Range("D4:AJ1000").ClearContents
 Application.DisplayAlerts = False
 Application.ScreenUpdating = False
 Application.Calculation = xlCalculationManual
 Cnt2 = 1 'dimension array
 Cnt3 = 0 'array positions
 
 Set FSO = CreateObject("scripting.filesystemobject")
 '***change Folder path/name to your folder path/name
 Set FlDr = FSO.GetFolder("I:\Accounts\2018\Financial Reporting\BRD\NewRev\Files")
 For Each FileNm In FlDr.Files
 If FileNm.Name Like "*.xlsm" Then
 Workbooks.Open Filename:=FileNm
 For Each sht In Workbooks(FileNm.Name).Sheets
 If sht.Name = "CC" Then
 Cnt2 = Cnt2 + 1
 ReDim Preserve RngArr(Cnt2)
 ReDim Preserve RngArr2(Cnt2)
 With Workbooks(FileNm.Name).Sheets(sht.Name)
 Set Rng = .Range(.Cells(25, "E"), .Cells(25, "AK"))
 Set Rng2 = .Range(.Cells(40, "E"), .Cells(40, "AK"))
 End With
 RngArr(Cnt3) = Rng
 RngArr2(Cnt3) = Rng2
 Cnt3 = Cnt3 + 1
 Exit For
 End If
 Next sht
 Workbooks(FileNm.Name).Close SaveChanges:=False
 End If
 Next FileNm
 
Cnt = 4
For Cnt4 = 0 To Cnt3 - 1
With ThisWorkbook.Sheets("TAB1")
.Range(.Cells(Cnt, "D"), .Cells(Cnt, "AJ")) = RngArr(Cnt4)
.Range(.Cells(Cnt + 1, "D"), .Cells(Cnt + 1, "AJ")) = RngArr2(Cnt4)
End With
Cnt = Cnt + 2
Next Cnt4
 
 MsgBox "Finished Files"
 
 LastRow = Sheets("TAB1").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 Sheets("TAB1").Sort.SortFields.Clear
 Sheets("TAB1").Sort.SortFields.Add Key:=Range("D5:D" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
 With Sheets("TAB1").Sort
 .SetRange Range("D5:AJ" & LastRow)
 .Header = xlGuess
 .MatchCase = False
 .Orientation = xlTopToBottom
 .SortMethod = xlPinYin
 .Apply
 End With
 Application.DisplayAlerts = True
 Application.ScreenUpdating = True
 Application.Calculation = xlAutomatic
 Application.Cursor = xlDefault
 Set FlDr = Nothing
 Set FSO = Nothing
 Exit Sub
Erfix:
 On Error GoTo 0
 MsgBox "Error"
 Application.DisplayAlerts = True
 Application.ScreenUpdating = True
 Application.Calculation = xlAutomatic
 Application.Cursor = xlDefault
 Set FlDr = Nothing
 Set FSO = Nothing
 End Sub
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Morning Dave!

I do apologise for not seeing your reply as it has improved the performance of the macro hugely.

I've tested it on 110 files and it processed this in under 5 minutes which is amazing!! I'm planning to test again with up to 200 files and I'll report back with the times.

Thanks as always!!!!
Ryan
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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