4

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

我的用户表单

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

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

当我执行当前代码时,我得到'运行时错误'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
4

1 回答 1

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

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

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

If FormName.Controls(itm).Value = True
于 2012-07-03T10:19:03.320 回答