移动Sub并保持正确的范围(替换我。)

时间:2012-07-24 15:22:23

标签: vba excel-vba scope excel

我的应用程序中有一个Sub,当前位于名为FRMPFC_folderCreatorWindow的用户窗体中。为了清楚整个应用程序,我希望将此Sub从userform移动到名为PFC_filesystemManipulation的模块中,并通过FRMPFC_folderCreatorWindow中的按钮从那里调用Sub,但是,当我执行此操作并运行我的代码时,在该行生成错误:

For Each cCont In Me.Controls

我理解这是因为Sub已经被带到了表单的上下文之外,但是如何在不使用Me.Controls的情况下维护上下文?我猜我需要引用表单并使用FRMPFC_folderCreatorWindow.Controls,但由于大多数控件嵌套在框架中,我不确定我的当前代码是作用于表单还是仅作用于按钮所在的框架。任何帮助将不胜感激。

Private Sub PFC_createFolders(Basepath, currentControl, parentFolder, parentGroup)

Dim cCont As Control
Dim createSubFolder As String
Dim fs As Object

Set fs = CreateObject("Scripting.FileSystemObject")

'Check if the project folder already exists and if so, raise an error and exit
    MkDir Basepath & "\" & parentFolder

    'Create the superceded documents folder in every 2nd generation folder
    MkDir Basepath & "\" & parentFolder & "\" & "_Old versions"
    For Each cCont In Me.Controls
        If TypeName(cCont) = "CheckBox" Then
            If cCont.GroupName = parentGroup Then
                If cCont.Value = True Then
                    If cCont.Name <> currentControl Then
                        createSubFolder = cCont.Caption
                        NewFolder = Basepath & "\" & parentFolder & "\" & createSubFolder
                        If fs.folderexists(NewFolder) Then
                            'do nothing
                        Else
                            'Create 3rd generation folder
                            MkDir NewFolder

                            'Create the superceded documents folder in every 3rd generation folder
                            MkDir NewFolder & "\" & "_Old versions"

                            'Create hard-coded subfolders within Confirmit Exports
                            If createSubFolder = "Confirmit Exports" Then
                                MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Triple S"
                                MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Word Export"
                                MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Survey Definition"
                                MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Data"
                                MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Data" & "\" & "Early Data"
                                MkDir Basepath & "\" & parentFolder & "\" & createSubFolder & "\Data" & "\" & "Final Data"
                            End If

                        End If

                    End If

                End If

            End If

        End If

    Next cCont

   End Sub

1 个答案:

答案 0 :(得分:0)

我刚试验过这个并用以下代码替换Me.Controls:

    FRMPFC_folderCreatorWindow.Controls