Selected Answer
Leopard
The problem is that your sub is told to accept four strings:
Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
ByVal CopyFromSheetName As String, ByVal ExportToSheetName As String)
but in your export_data sub you redefine 3 of the variables as Variants:
Sub export_data()
Dim Filename As Variant, CopySheet As Variant, ExportSheet As Variant
(but you assign a string to Filename). VBA fails since you're trying to pass Variants (the arrays) when the OpenFilesFromFolder sub expects only Strings.
In the first attached file (newer file format), I've changed the Dim statement in export_data to read:
Sub export_data()
Dim Filename As String, FolderPath As String, CopySheet As Variant, ExportSheet As Variant
and your sub to accept the same:
Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
ByVal CopyFromSheetName As Variant, ByVal ExportToSheetName As Variant)
so you pass it the data types it expects.
REVISION 25 March 2023:
In the second file attached, I've modified your second macro so it copies data from the source sheets to the destination sheets set in your arrays. I've used the method mentioned in the Discussion below (on your further coding error) to determine the sheets (e.g. CopyFromSheetName(n)) and allowing more than 2 sheets per array- should you need them:
Sub OpenFilesFromFolder(ByVal ExportToFileName As String, ByVal FolderPath As String, _
ByVal CopyFromSheetName As Variant, ByVal ExportToSheetName As Variant)
Dim ExtBk As Workbook, IntBk As Workbook
Dim FilePath As String
Dim lRow As Long
' Dim Rng(1 To 2) As Range
On Error GoTo myerror
Set IntBk = ActiveWorkbook
FilePath = Dir(FolderPath & ExportToFileName)
If FilePath <> "" Then
Application.ScreenUpdating = False
Set ExtBk = Workbooks.Open(FolderPath & FilePath, 0, False)
' loop through all items in the arrays
For n = LBound(CopyFromSheetName) To UBound(CopyFromSheetName)
' choose a sheet from the array to copy
With IntBk.Worksheets(CopyFromSheetName(n))
lRow = .Cells(Rows.Count, 1).End(xlUp).Row
'clear any old values in a sheet:
With ExtBk.Worksheets(ExportToSheetName(n))
.Range("A2:F" & .Cells(.Rows.Count, 1).End(xlUp).Row).ClearContents
End With
'Copy from source:
ExtBk.Worksheets(ExportToSheetName(n)).Range("A1:F" & lRow).Value = .Range("A1:F" & lRow).Value
End With
Next n
Else
'file not found
Err.Raise 53, , ExportToFileName & Chr(10) & "File Not found"
End If
myerror:
Application.DisplayAlerts = False
If Not ExtBk Is Nothing Then ExtBk.Close Err = 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub
I have to say that I find your code overly complicated, given you appear to be just copying sheets from one book to another. A single macro might achieve the same effect but I've stuck with your method andassume you will do more in your final macro.
Hope this fixes both problems now.