Hello, I am a novice to VBA, but I'm getting there.
I am trying to build a program, and one of its aspect is to use two-dimensional arrays.
One of the functions get directories, where it fetches an array of folder directories, an array of spreadsheet directories, and then stores both of those arrays into one return array. The end-goal is to use both of them to find all spreadsheets in a given directory through a recursive subroutine.
The way I'm going about this is (See latter half of code):
I have tried to access this array, but it seems like I can only access its bounds, so something like this works:
However, this does not work, and from what I have read it should be the proper syntax for multi-dimensional arrays:
So my question is: did I create a two-dimensional array correctly? It gives no compile errors, so I assume so. How would I access the elements within the arrays of the array that I created?
Keep in mind that I am a novice to VBA, Microsoft's docs are my best friend... most of the time.
I am trying to build a program, and one of its aspect is to use two-dimensional arrays.
One of the functions get directories, where it fetches an array of folder directories, an array of spreadsheet directories, and then stores both of those arrays into one return array. The end-goal is to use both of them to find all spreadsheets in a given directory through a recursive subroutine.
The way I'm going about this is (See latter half of code):
VBA Code:
Function GetDirectory(FileDirectory As String)
' variables
Dim FolderName As String
Dim FolderArray As Variant
Dim FileArray As Variant
Dim DirectoryName As String
' initialize arrays
FolderArray = Array()
FileArray = Array()
' create first entry
' specifies directories, folders, files as read-only, and no-attribute files
DirectoryName = Dir(FileDirectory & "\*", 17)
' pass the first two DOS directories "." and ".."
DirectoryName = Dir()
DirectoryName = Dir()
' Get folders and spreadsheets from directory
Dim FolderArrayIndex As Integer
Dim FileArrayIndex As Integer
FolderArrayIndex = 0
FileArrayIndex = 0
Do While DirectoryName <> ""
' if the file is a folder (absence of extension)
If InStr(DirectoryName, ".") = 0 Then
' expand the array, creating another slot for another element
ReDim Preserve FolderArray(FolderArrayIndex)
FolderArray(FolderArrayIndex) = DirectoryName
FolderArrayIndex = FolderArrayIndex + 1
' else if the file is a spreadsheet
ElseIf InStr(LCase(DirectoryName), ".xls") > 0 Then
' expand the array, creating another slot for another element
ReDim Preserve FileArray(FileArrayIndex)
FileArray(FileArrayIndex) = DirectoryName
FileArrayIndex = FileArrayIndex + 1
End If
' fetch next element
DirectoryName = Dir()
Loop
' load arrays into a master array and return
GetDirectory = Array(FolderArray, FileArray)
End Function
I have tried to access this array, but it seems like I can only access its bounds, so something like this works:
VBA Code:
Dim MasterArray As Variant
MsgBox(UBound(MasterArray))
VBA Code:
Dim MasterArray As Variant
MsgBox(MasterArray(1, 1)
Keep in mind that I am a novice to VBA, Microsoft's docs are my best friend... most of the time.