gpt4 book ai didi

vba - 将子目录添加到动态数组

转载 作者:行者123 更新时间:2023-12-01 15:40:54 25 4
gpt4 key购买 nike

我正在制作一个将子目录添加到数组的宏。子目录的个数是未指定的,所以我每次都将数组重新调整为数组计数器的大小。当我尝试将数组值设置为索引值“count”时,我得到:编译错误:预期数组。

我试图让我的代码尽可能简单,在我看来它应该可以工作,但显然不行。有人介意指出我搞错了吗?

'DEFINE PATH AND SUBDIRECTORY DIR
Path = "C:\Users\MyComp\Documents\test\folders\"
Dim SubDir
SubDir = Dir(Path, vbDirectory)



'CREATE ARRAY AND COUNTER
Dim folderindex As Long
Dim count As Integer
count = 0



'ADD SUBDIRECTORY PATHNAMES TO ARRAY
Do Until SubDir = ""
folderindex(count) = Path & SubDir
count = count + 1
ReDim Preserve folderindex(count)
SubDir = Dir()
Loop



End Sub

谢谢你的帮助

最佳答案

当你声明成为数组的变量时,你需要以这种方式使用括号:

Dim folderindex() As String

重要!!此外,您需要将变量声明为 As String(类型),因为您要在其中保留文本,而不是数字。

关于vba - 将子目录添加到动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21243374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com