Hi, I'm new to the site and also VBA coding.

I'm using the code highlighted in blue below currently, but it doesn't quite suit my needs. The code below successfully loops through my files, extracts the data and adds it to a new sheet side by side with a reference of the file name above each entry. What I'm trying to achieve, is for the code to use the open (consolidation file), loop through each data file, extract from specific cells and then past into individual tabs within the open/active workbook. I'm struggling to workout, a) how to include code to use the current Open/Active workbook (consolidation file), b) how to alter the code from Range ().CurrentRegion.Select to pick up individual cells (fairly random) and place into a specificed tab (sheet) within the active workbook.

For example.

Data File: New Zealand.xlsx - extracted data goes to Workbook (Consolidation File.xlsm) and Worksheet (New Zealand)

The data within each file is setout in the sameway, so would be not variation between ranges for each sheet. Within the data files, there are various tabs, but for my needs only using a specific tab called "Input Comments(SPI)".

If anyone could help with this it would be much appreciated. Either additional code which can be added to the below macro Or an alternative code which is more flexible.


Sub Consolidation_a()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceCcount As Long, Fnum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim Cnum As Long, CalcMode As Long

'Fill in the path\folder where the files are
MyPath = "H:\Departmental Drive\Marketing\RSP&I\Archive\Luke Ormerod Folder\TEST Consolidation (SOP)"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xlsx*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()

'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
Cnum = 1

'Loop through all files in the array(myFiles)
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))
On Error GoTo 0

If Not mybook Is Nothing Then

On Error Resume Next
Set sourceRange = mybook.Worksheets("Input Comments(SPI)").Range("B2").CurrentRegion

If Err.Number > 0 Then
Set sourceRange = Nothing
'if SourceRange use all rows then skip this file
If sourceRange.Rows.Count >= BaseWks.Rows.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceCcount = sourceRange.Columns.Count

If Cnum + SourceCcount >= BaseWks.Columns.Count Then
MsgBox "Sorry there are not enough columns in the sheet"
mybook.Close savechanges:=False
GoTo ExitTheSub

'Copy the file name in the first row
With sourceRange
BaseWks.Cells(1, Cnum). _
Resize(, .Columns.Count).Value = MyFiles(Fnum)
End With

'Set the destrange
Set destrange = BaseWks.Cells(2, Cnum)

'we copy the values from the sourceRange to the destrange
With destrange
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
destrange.Value = sourceRange.Value

Cnum = Cnum + SourceCcount
End If
End If
mybook.Close savechanges:=False
End If

Next Fnum
End If

'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With

End Sub