2017-08-09 44 views
0

我有用戶窗體'CemeaFinallist',其中有複選框和按鈕。我想用複選框名稱的值作爲變量= CNN在 'Normal.newmacros.minipro'在普通宏中使用userform值

enter image description here

以下是用戶窗體按鈕腳本

Private Sub Shift_Click() 

CemeaFinallist.Hide 
Dim ctl As Control 
Dim j As Long 
For Each ctl In Me.Controls 
If TypeOf ctl Is MSForms.CheckBox Then 
    If Me.Controls(ctl.Name).Value = True Then 

If ctl.Caption = "Select All" Then 
Else 

Application.Run MacroName:="Normal.NewMacros.minipro" 

End If 
End If 
End If 

Next 
Application.ScreenUpdating = True 
End Sub 

以下是Normal.NewMacros宏觀

Sub MiniPRO() 
Application.ScreenUpdating = False 
Dim path As String 
Dim CNN As String 
Dim ex As String 
Dim News As String 
Dim SD As String 


path = "C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\EMEA FOR DAILY USE\" 
CNN = ctl.Name 'at this stage Run Time Error '424' Object required' 
ex = ".DOCX" 

Documents.Open FileName:=path & CNN & ex 
+0

哪部分代碼失敗? – jsotola

+0

需要運行時錯誤'424'對象。在'CNN = ctl.Name' –

+1

你沒有聲明'ctl',所以它的類型是Variant,和'Dim ctl as Variant'一樣。您尚未爲其分配任何值,但您試圖通過檢索Name屬性將其用作對象。錯誤表示沒有找到任何對象。你需要在MiniPRO中定義'ctl'對象'dim ctl as object''set ctl = forms(「CemeaFinallist」)。checkbox' ....不是確切的代碼,而是沿着這些行的東西 – jsotola

回答

1

在您的用戶窗體中,使用:

Application.Run MacroName:="NewMacros.MiniPRO", varg1:=ctl.Name 

在Normal.NewMacros模塊,使用:

Function MiniPRO(ByVal CtlName as String) 
    Application.ScreenUpdating = False 
    Dim path As String 
    Dim CNN As String 
    Dim ex As String 
    Dim News As String 
    Dim SD As String 


    path = "C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\EMEA FOR DAILY USE\" 
    CNN = CtlName 
    ex = ".DOCX" 

    Documents.Open FileName:=path & CNN & ex 
    '... 
End Function 

您還可以通過簡單的更換測試If Me.Controls(ctl.Name).Value = True ThenIf ctl.value = True Then既然你已經有了一個對控件的引用。

+0

編譯錯誤:語句錯誤'Application.Run MacroName:=「Normal.NewMacros.MiniPRO」,ctl.Name' –

+0

好吧,可能是因爲第一個參數是顯式的,第二個不是。嘗試'Application.Run MacroName:=「Normal.NewMacros.MiniPRO」,varg1:= ctl.Name' –

+0

我已經添加了我已經在Userform中使用的整個代碼嗯,看看 –