I have a group of files in a folder called 'inputs'. Each file contains several sheets laid out in exactly the same way, but containing different values. Each of these files also has a summary sheet listing all the sheet names in column A.
I need to
-open each file in turn -pull the values from each sheet into another book I have named 'consolidator' & calculate the consolidator sheet based on these values. -then copy and paste the result into an output file and save it. -I need to do this for every sheet in the book, and then for every file in the folder.
My code therefore contains a loop (to go through each sheet), within another loop (to go through each file).
THE PROBLEM is that my code runs and generates the correct output, if the sheet names in the files are the same (even if the file names are different).
However, if the sheet names in each file are different, on the second iteration of my 'file' loop, the code is interrupted with 'cannot find sheet name x in file name y (file name y in this instance has not changed from the first iteration).
Thanks in advance if you are able to help! :)
Here is my code:
Sub FileExtractor()
'SET KEY VARIABLES
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim MyPathOutput As String
Dim i As Long
Dim LastRow As Long
Dim rngi As Range
Dim strx As String
Dim StrLen1 As Integer
Dim StrLen2 As Integer
calcsetting = Application.Calculation
'DEFINE FILE LOCATIONS
MyPath = ThisWorkbook.Path
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\Inputs\"
Else
MyPath = MyPath & "Inputs\"
End If
' Change this to the path\folder location of your output file.
MyPathOutput = ThisWorkbook.Path
' If there are no Excel files in the responses folder, exit.
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
' Fill the myFiles array with the list of Excel files
' in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
' This sets various application properties. NB. Calculation mode is set to off, so all calculations must be forced
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
.DisplayAlerts = False
End With
'define the name of the workbook that is running the macro
Set masterwks = ThisWorkbook
'BEGIN WORKING THROUGH FILES TO CONSOLIDATE INFO
'set row number where data will start to be pasted
cnum = 1
rnum = 1
' Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
If Not mybook Is Nothing Then
On Error Resume Next
masterwks.Worksheets("Consolidator").Activate
'update consolidator with new input file name
newname = "[" & mybook.Name & "]"
With masterwks.Worksheets("Consolidator")
Currentname = .Range("filename")
.Cells.Replace What:=Currentname, Replacement:= _
newname, LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
LastRow = mybook.Sheets("summary").Range("A:A").Find("*", searchdirection:=xlPrevious).Row
For i = 4 To LastRow
Set rngi = mybook.Sheets("summary").Range("A" & i)
StrLen1 = Len(rngi.Value)
StrLen2 = StrLen1 - 1
strx = Trim(Left(rngi.Value, StrLen2))
newname2 = strx
With masterwks.Worksheets("Consolidator")
Currentname2 = .Range("sheetname")
.Cells.Replace What:=Currentname2, Replacement:= _
newname2, LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
Set sourceRange = masterwks.Sheets("consolidator").Range("outputrange")
Calculate
'CREATE OUTPUT FILE
' Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
With BaseWks
Sheets(1).Select
Cells(1, 1).Select
End With
'PASTE FILE DATA
' Set the destination range to A
Set destrange = BaseWks.Range("A" & rnum)
sourceRange.Copy
destrange.PasteSpecial Paste:=xlPasteValues, Transpose:=False
destrange.PasteSpecial Paste:=xlPasteFormats, Transpose:=False
BaseWks.Columns.AutoFit
BaseWks.SaveAs Filename:=MyPathOutput & "\" & masterwks.Sheets("consolidator").Range("sheetname") & " - " & mybook.Name
Next i
End If
mybook.Close savechanges:=False
Next FNum
End If
Application.Calculation = calcsetting
ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
MsgBox "Ta da!!"
Application.Calculation = calcsetting
End Sub