我正在尝试使用表单输入目录,父文件夹名称,然后通过选项卡选择子文件夹将使用一系列复选框在父文件夹中显示。
我可以输入驱动器,项目名称和项目编号,首先检查父文件夹是否存在以及是否创建它。
然后检查“在线使用”复选框是否处于活动状态,如果是,则在“在线”选项卡中创建所有其他复选框的名称数组。然后它变得棘手,因为我想循环遍历每个复选框名称以检查每个复选框名称是否处于活动状态,如果是,我想抓住每个复选框的“标题”并使用它在父目录中创建子文件夹(如果它还不存在)。
当我执行当前代码时,我得到'运行时错误'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
答案 0 :(得分:7)
...
onlineSubfolders = Array("online_progCosts", "online_exports")
...
For Each itm In onlineSubfolders
If itm.Value = True Then
...
数组的每个元素都是一个字符串。字符串没有.Value
属性。
我猜这些是您的复选框的名称,因此您需要通过引用控件本身来获取值。我不知道你的表格是什么。
If FormName.Controls(itm).Value = True