Search code examples
arraysexcelvbastringbyref

Error 13 when passing an array to a function that should fill it


First of all: I am fairly new to VBA, so please excuse me if the question is rather trivial, but this mistake has been keeping me busy all day - I have absolutely no clue.

I am working on a small macro to look through a folder, count the files and fill a 2d array with the full file name and a specific section of the name. So I am creating an array in my main sub and call the function that takes the empty array as a parameter and fills it.

My macro looks somewhat like this:

Private Sub whatever()
    Dim arr(10, 2) As String
    Dim count As Integer
    CheckFolder(arr, "somepath", count)
End Sub

Sub CheckFolder(ByRef arr() As String, strPath As String, count As Integer)

    Dim fso, oFolder, oSubfolder, oFile, queue As Collection
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim fileCount As Integer
    Dim temp(10, 2) As String
    fileCount = 1

    WriteToLog "zähle Files in Ordner " & strPath & "... "

    Dim path As String
    path = ActiveWorkbook.path & "\" & strPath
    Set queue = New Collection
    queue.Add fso.GetFolder(path) '- Pfad zum Ordner

    Do While queue.count > 0
        Set oFolder = queue(1)
        FolderName = fso.GetFileName(oFolder)
        queue.Remove 1 'dequeue
        For Each oSubfolder In oFolder.SubFolders
          queue.Add oSubfolder 'enqueue
        Next oSubfolder
        For Each oFile In oFolder.Files
            Filename = fso.GetFileName(oFile)
            '- my guess is the next two line are the problem?
            temp(fileCount, 1) = Filename
            temp(fileCount, 2) = StringCutter(Filename)
            fileCount = fileCount + 1
        Next oFile
    Loop

    arr = temp
    count = fileCount

End Sub

I am not sure, but I think that those two lines are the problem (as the rest of the logic worked perfectly fine before)

temp(fileCount, 1) = Filename
temp(fileCount, 2) = StringCutter(Filename)

The function "StringCutter" that is called here, returned a substring of the filename. I tested that function before and I works, so I don't think it is causing any problem.

I would be much appreciated if someone could tell me where my mistake is.

EDIT: this is the StringCutter function, that takes a string and cuts out a certain portion of it and returns this portion. As mentioned before, this function works perfectly fine when I use it outside of filling an array.

Function StringCutter(str As String) As String

    Dim ret As String
    Dim retLen As Integer
    Dim pos As Integer

    retLen = Len(str)
    ret = Right(str, (retLen - 31))
    pos = InStr(ret, "_")

    If (pos > 0) Then
        ret = Left(ret, (pos - 1))
    Else
        ret = Left(ret, 4)
    End If

    StringCutter = ret

End Function

I hope that helps


Solution

  • I think I figured it out! I was using the variable "Filename" which I guess is from the oFile element, because I didn't create it. Maybe that's why the types weren't compatible. Creating a Dim fileName AS String and using this variable here:

    For Each oFile In oFolder.Files
            fileName = fso.GetFileName(oFile)
            temp(fileCount, 1) = fileName
            temp(fileCount, 2) = StringCutter(fileName)
            fileCount = fileCount + 1
     Next oFile
    

    solved the problem. Thank you all for your help! :)