arrays - 迭代变体数组

标签 arrays vba excel for-loop

我正在尝试使用表单输入目录、父文件夹名称,然后通过选项卡使用一系列复选框选择哪些子文件夹将出现在父文件夹中。

My user form

我可以输入驱动器、项目名称和项目编号,首先检查父文件夹是否存在,如果不存在则创建它。

然后,我检查“使用在线”复选框是否处于事件状态,如果是,则创建“在线”选项卡中所有其他复选框的名称数组。然后它变得很棘手,因为我想循环遍历每个复选框名称以检查每个复选框是否处于事件状态,如果是,我想获取每个复选框的“标题”并使用它在父目录中创建一个子文件夹(如果它尚不存在)。

当我执行当前代码时,我收到“运行时错误'424'需要对象”和行

    If itm.Value = True Then

以黄色突出显示。

用于此用户表单的“创建文件夹”部分的所有代码都可以在下面找到:

Private Sub create_folders_button_Click()

'Create a variable for the drive letter
Dim driveLetter As String
driveLetter = drive_list.Value

'Create a variable for the project name
Dim projectName As String
projectName = p_name_textbox.Value

'Create a variable for the project number
Dim projectNumber As String
projectNumber = p_number_textbox.Value

'Create a variable for the constructed BasePath
Dim BasePath As String

'Create a new file system object for handling filesystem manipulation
  Set fs = CreateObject("Scripting.FileSystemObject")

'Populate an array with the online subfolders
  Dim onlineSubfolders As Variant
  onlineSubfolders = Array("online_progCosts", "online_exports")

'Compile the basePath

  BasePath = driveLetter & projectName & " (" & projectNumber & ")"

'Check if the project folder already exists and if so, raise an error and exit
    If Dir(BasePath, vbDirectory) <> "" Then
        MsgBox BasePath & " already exists", , "Error"
    Else
        'Create the project folder
        MkDir BasePath
        MsgBox "Parent folder creation complete"

        If online_toggle.Value = True Then
            Dim online As String
            online = "Online"
            MkDir BasePath & "\" & online

            Dim itm As Variant
            For Each itm In onlineSubfolders
                If folder_creator_window.Controls(itm).Value = True Then
                    Dim createFolder As String
                    createFolder = folder_creator_window.Controls(itm).Caption
                    NewFolder = BasePath & "\" & online & "\" & createFolder
                    If fs.folderexists(NewFolder) Then
                        'do nothing
                    Else
                        MkDir NewFolder
                    End If

                Else
                    'do nothing
                End If

            Next itm

        Else
            MsgBox "The online folder was not created because it was not checked"

        End If

    End If

End Sub

最佳答案

...
onlineSubfolders = Array("online_progCosts", "online_exports")
...
For Each itm In onlineSubfolders
            If itm.Value = True Then
...

数组的每个元素都是一个字符串。字符串没有 .Value 属性。

我猜这些是您的复选框的名称,因此您需要通过引用控件本身来获取值。我不知道你的表格叫什么。

If FormName.Controls(itm).Value = True

关于arrays - 迭代变体数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11308667/

相关文章:

javascript - 循环遍历数组以分解 JavaScript 代码

ios - UITableView-从两个数组切换数据

arrays - 用另一个文件替换一个文件中的所有字符串实例

vba - 如何在 VBA 中创建列表(而不是字典)?

excel - 具有长路径名的 Excel 上的 Shell 命令不起作用

c# - 来自 COM 插件的 SheetBeforeDoubleClick

vba - 如何过滤列中的4个数字

java - 为什么我得到 - ArrayIndexOutOfBoundsException : 5?

excel - 使用 VB/VBA 搜索 Outlook 邮件并将特定数据提取到 Excel 工作表中

java - Apache POI 在空电子表格中看到列?