vba 遍历变体数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11308667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterating through a variant array
提问by jezzipin
I am trying, with a form, to enter a directory, a parent folder name and then select via tabs which sub-folders will feature within the parent folder using a range of checkboxes.
我正在尝试使用表单输入目录、父文件夹名称,然后通过选项卡选择哪些子文件夹将使用一系列复选框在父文件夹中显示。
I can enter the drive, project name and project number which first checks if the parent folder exists and if not creates it.
我可以输入驱动器、项目名称和项目编号,首先检查父文件夹是否存在,如果不存在则创建它。
I then check if the 'Use Online' checkbox is active and if so create an array of the names of all of the other checkboxes within the 'Online' tab. It then gets tricky because I want to loop through each of the checkbox names to check whether each one is active and if so, I want to grab the 'caption' of each checkbox and use it to create a sub-folder within the parent directory (if it does not already exist).
然后我检查“使用在线”复选框是否处于活动状态,如果是,则在“在线”选项卡中创建所有其他复选框的名称数组。然后它变得棘手,因为我想遍历每个复选框名称以检查每个复选框是否处于活动状态,如果是,我想获取每个复选框的“标题”并使用它在父目录中创建一个子文件夹(如果它不存在)。
When I execute my current code I get 'Run-time error '424' Object required and the line
当我执行我当前的代码时,我得到“运行时错误‘424’所需的对象和行
If itm.Value = True Then
highlighted in yellow.
以黄色突出显示。
All of the code used for the 'create folders' part of this user form can be found below:
可以在下面找到用于此用户表单的“创建文件夹”部分的所有代码:
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
回答by Andrew Leach
...
onlineSubfolders = Array("online_progCosts", "online_exports")
...
For Each itm In onlineSubfolders
If itm.Value = True Then
...
Each element of the array is a string. Strings don't have .Value
properties.
数组的每个元素都是一个字符串。字符串没有.Value
属性。
I guess those are the names of your checkboxes, so you need to get the value by referencing the control itself. I don't know what your form is called.
我猜这些是您的复选框的名称,因此您需要通过引用控件本身来获取值。我不知道你的表格叫什么。
If FormName.Controls(itm).Value = True