Iterating through a variant array

21,864
...
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.

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
Share:
21,864
jezzipin
Author by

jezzipin

Software developer at Tempcover

Updated on July 30, 2022

Comments

  • jezzipin
    jezzipin almost 2 years

    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.

    My user form

    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

        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