matrix -for and -each statements vba



我之前得到过帮助,并在这里用vba显示了特定文件夹中特定文件类型的数组。我想将其开发成矩阵,因为我还有几个文件夹包含这些子文件夹

这部分是我想象的矩阵的样子我看到的唯一例子建议在excel中使用一个表,哪个vba可以使用-next命令来移动它的(x,y)坐标。我想知道是否有一种方法可以通过vba中的语法来做到这一点,而不必参考excel中的表?

upperFolders = Array("foldernames1", "foldernames2", "foldernames3",
folderNames1 = Array("Folder1", "Folder2", "Folder3")
folderNames2 = Array("Folder4", "Folder5", "Folder6")
folderNames3 = Array("Folder7", "Folder8", "Folder9")
mainFolder = "C:xxxxxx"

在上一篇文章中给我的矩阵的完整代码

Dim Myfile As String, MyFolder As String, NewFile As String, SubFolder As String
Sub SpecificFileTypeInSpecificFolders()
'
Dim myFile As String
Dim subFolder As Variant 
Dim folderNames As Variant
Dim mainFolder As String

'想用矩阵代替下面的

folderNames = Array("Folder1", "Folder2", "Folder3")

"代码的其余部分,因为它被赋予了

mainFolder = "C:xxxxxx"
For Each subFolder In folderNames
    MsgBox subFolder
    myFile = Dir(mainFolder & subFolder & "*.csv")
    Do While myFile <> ""
        MsgBox myFile
        myFile = Dir
    Loop
Next subFolder
End Sub

你是说这种事情吗?:

Function MakeMatrix(ParamArray Arrays() As Variant) As Variant
    'assumes that function is passed a collection of 0-based arrays
    'all with the same upper bound
    'returns a 0-based 2-dimensional variant array with the same values
    Dim Matrix As Variant
    Dim m As Long, n As Long, i As Long, j As Long
    m = UBound(Arrays)
    n = UBound(Arrays(0))
    ReDim Matrix(0 To m, 0 To n)
    For i = 0 To m
        For j = 0 To n
            Matrix(i, j) = Arrays(i)(j)
        Next j
    Next i
    MakeMatrix = Matrix
End Function
Sub test()
    Dim folderNames1, folderNames2, folderNames3 'all variant
    Dim myMatrix As Variant
    folderNames1 = Array("Folder1", "Folder2", "Folder3")
    folderNames2 = Array("Folder4", "Folder5", "Folder6")
    folderNames3 = Array("Folder7", "Folder8", "Folder9")
    myMatrix = MakeMatrix(folderNames1, folderNames2, folderNames3)
    Debug.Print myMatrix(1, 1)
    Debug.Print myMatrix(2, 2)
End Sub

输出:

Folder5
Folder9

相关内容

  • 没有找到相关文章

最新更新