Type Mismatch Error

CoconutP

New Member
Joined
Aug 11, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I am trying to perform a process for 2 files I am naming specifically. Since there are 2 files, I am declaring parts of the folder names and file names as an array. But I think the mixing of arrays and strings are giving me the Type Mismatch error message. Or my construction below is just completely wrong for looping through multiple files? How should I correct the below? Thanks in advance!

VBA Code:
Sub Mgmtaccts()

Dim FullName As Object
Dim FileNames As Object
CurrentFolderYear = Sheet1.Range("B4").Value
CurrentFolderMonth = Sheet1.Range("B5").Value
CurrentFileMonth = Sheet1.Range("B6").Value

Dim EntityName As String, FolderName As String, MgmtAcctsName As String, PathName As String
EntityName = Array("ABC LP", "ABC (GP)")
FolderName = Array("Folder ABC LP", "Folder ABC (GP)")
PathName = "\\Server\Monthly " & CurrentFolderYear & "\" & CurrentFolderMonth & "\Other\" & FolderName
MgmtAcctsName = EntityName & " Management Accounts " & CurrentFileMonth

FullName = PathName & "\" & MgmtAcctsName & ".xlsx"

For Each FullName In FileNames
Workbooks.Open FileName:=FullName

Insert code here

Next FullName

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You are declaring "FullName" and "FileNames" as Objects, but then you seem to be setting "FullName" equal to a string here:
VBA Code:
FullName = PathName & "\" & MgmtAcctsName & ".xlsx"

And you have this line here:
VBA Code:
For Each FullName In FileNames
though you have not set "FileNames" equal to anything yet!
 
Upvote 0
Perhaps if you restructured a bit (not tested).

VBA Code:
Sub Mgmtaccts()
    Dim FullName As String, MgmtAcctsName As String, PathName As String
    Dim CurrentFolderYear, CurrentFolderMonth, CurrentFileMonth 'previously undefined variables
    Dim I As Long, J As Long
    Dim EntityName As Variant, FolderName As Variant
    
    CurrentFolderYear = Sheet1.Range("B4").Value
    CurrentFolderMonth = Sheet1.Range("B5").Value
    CurrentFileMonth = Sheet1.Range("B6").Value
    
    EntityName = Array("ABC LP", "ABC (GP)")
    FolderName = Array("Folder ABC LP", "Folder ABC (GP)")
    
    With CreateObject("Scripting.FileSystemObject")
        For I = LBound(FolderName) To UBound(FolderName)
            For J = LBound(EntityName) To UBound(EntityName)
                PathName = "\\Server\Monthly " & CurrentFolderYear & "\" & CurrentFolderMonth & "\Other\" & FolderName(I)
                MgmtAcctsName = EntityName(J) & " Management Accounts " & CurrentFileMonth
                FullName = PathName & "\" & MgmtAcctsName & ".xlsx"
                
                If .FileExists(FullName) Then
                    Debug.Print "Open file '" & FullName
                    Workbooks.Open Filename:=FullName
                Else
                    Debug.Print "File '" & FullName & "' does not exist"
                End If
            Next J
        Next I
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
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