I have adapted a code from RDB for my use where I want to merge multiple files into one workbook. I've read through other posts but I can't find the same issue I am having. I am running Mac OS12 and Excel 2019.
The problem I am running into is Mybook does not change from Nothing to the file I want to open even though MyFiles(Fnum) shows the correct file. Any help with this would be greatly appreciated. Thanks!
VBA Code:
Sub Basic_Dir_Example_Mac()
'Ron de Bruin, 27-Feb-2019
'Only for Mac Excel 365 with the latest updates
Dim MyPath As String, FilesInPath As String
Dim Fnum As Long, MyFiles() As String
Dim Nwb As Workbook
Dim Mybook As Workbook
Dim rnum As Long
Dim sourceRange As Range
Dim destrange As Range
Dim SourceRcount As Long
Dim CalcMode As Long
On Error Resume Next
MyPath = MacScript("return posix path of (choose folder with prompt ""Select the folder"") as string")
If MyPath = "" Then Exit Sub
On Error GoTo 0
rnum = 4
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.csv*")
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()
Loop
'Loop through all files in the array(myFiles)
If Fnum > 0 Then
' Add a new workbook to copy the list of files in
Set Nwb = Workbooks.Add
With Nwb.Sheets(1).Range("A3:I3")
.Value = Array("384 well Plate", "384 Well", "96 Well", "96 well ID", "Barcode", "Well ID", "665 nm", "620 nm", "Ratio")
.Font.Bold = True
End With
For Fnum = LBound(MyFiles) To UBound(MyFiles)
On Error Resume Next
Set Mybook = Nothing
On Error Resume Next
[B]Set Mybook = Workbooks.Open(MyFiles(Fnum))[/B]
On Error GoTo 0
If Not Mybook Is Nothing Then
On Error Resume Next
With Mybook.Worksheets(1)
Set sourceRange = .Range("B4:F387")
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= Mybook.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= Mybook.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
Mybook.Columns.AutoFit
Mybook.Close savechanges:=False
GoTo ExitTheSub
Else
Set destrange = Nwb.Sheets(1).Range("E" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
Mybook.Close savechanges:=False
End If
Next Fnum
Nwb.Sheets(1).Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub