2012-07-03 164 views
4

我想用一個表單來輸入一個目錄,一個父文件夾名稱,然後通過選項卡選擇使用一系列複選框在父文件夾中使用哪些子文件夾。遍歷變量數組

My user form

我可以輸入,如果父文件夾存在首先檢查驅動器,項目名稱和項目數量,如果沒有創建它。

然後檢查'Use Online'複選框是否處於活動狀態,如果是這樣,請在'Online'選項卡中創建所有其他複選框的名稱數組。然後它會變得棘手,因爲我想遍歷每個複選框名稱以檢查每個複選框是否活動,如果是,我想抓住每個複選框的「標題」並使用它在父目錄中創建一個子文件夾(如果它不存在)。

當我執行我當前的代碼我得到「運行時錯誤‘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 

回答

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

該數組的每個元素是一個字符串。字符串沒有.Value屬性。

我想這些是你的複選框的名稱,所以你需要通過引用控件本身來獲取值。我不知道你的表單被稱爲什麼。

If FormName.Controls(itm).Value = True