Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将数据加载到Veriable UserForms&;控制_Vba_Excel - Fatal编程技术网

Vba 将数据加载到Veriable UserForms&;控制

Vba 将数据加载到Veriable UserForms&;控制,vba,excel,Vba,Excel,我有一个运行在excel工作表(T_列表)中的表上的代码。代码的目的是将数据从tabl加载到用户窗体控件。用户窗体和控件是可验证的。打开时,代码会死机: 用户表单(FormName)。显示 错误消息为“类型不匹配”: 我做错了什么?如何调用要打开的veriable userform并将值保存到其控件中?这一点对我很有用-用于引用MSForms.userform对象。 但需要额外的权限 Excel选项>信任中心>宏设置>信任对 VBA项目对象模型 下面的函数返回与输入名称匹配的userform对象

我有一个运行在excel工作表(T_列表)中的表上的代码。代码的目的是将数据从tabl加载到用户窗体控件。用户窗体和控件是可验证的。打开时,代码会死机:
用户表单(FormName)。显示
错误消息为“类型不匹配”:


我做错了什么?如何调用要打开的veriable userform并将值保存到其控件中?

这一点对我很有用-用于引用MSForms.userform对象。 但需要额外的权限

Excel选项>信任中心>宏设置>信任对 VBA项目对象模型

下面的函数返回与输入名称匹配的userform对象

Public Function UserForms(FormName As String)
    Dim c As Object
    For Each c In ThisWorkbook.VBProject.VBComponents
        If c.Type = 3 And c.Name = FormName Then
            Set UserForms = c
            Exit For
        End If
    Next
End Function
并按如下方式修改您的代码:

Dim frm As Object
Set frm = UserForms(FormName)

If CurrValue = "Pass" Then
   frm.Show
   frm.Controls(Control_Yes) = True
   frm.Controls(Control_No) = False
ElseIf CurrValue = "Fail" Then
   frm.Show
   frm.Controls(Control_Yes) = False
   frm.Controls(Control_No) = True
End If

添加方法-不带其他函数

Dim frm As Object
Set frm = UserForms.Add(FormName)

If CurrValue = "Pass" Then
   frm.Show
   frm.Controls(Control_Yes) = True
   frm.Controls(Control_No) = False
ElseIf CurrValue = "Fail" Then
   frm.Show
   frm.Controls(Control_Yes) = False
   frm.Controls(Control_No) = True
End If

由于“信任访问VBA项目对象模型”选项因我的组织策略而被阻止,并且我无法使用下面建议的UserForms(FormName)功能,因此我使用保存用户名的数组解决了问题。 这是我的密码:

Public Sub Load_Data_to_Form()
Dim T_Test As Integer
Dim CurrRaw As Integer
Dim CurrValue As String
Dim FormName As String
Dim Control_Yes As String
Dim Control_No As String
Dim FormArray As Variant 'define array which hold userforms names
FormArray = Array(Test_Procedure1, Test_Procedure2, Test_Procedure3, Test_Procedure4)

T_Test = Sheets("T_list").Range("T1").Value   'Total raws in excel table
CurrRaw = Sheets("T_list").Range("N7").Value   'current raw

For i = 1 To T_Test 'running on test parameter table
    CurrValue = Sheets("Test_Data").Range("D_Start").Offset(CurrRaw, i + 7).Value
    FormName = Sheets("T_list").Range("T_Start").Offset(i, 6).Value
    Control_Yes = Sheets("T_list").Range("T_Start").Offset(i, 4).Value
    Control_No = Sheets("T_list").Range("T_Start").Offset(i, 5).Value

    For j = 0 To 3 'Running on 4 values in the FormArray
      If FormName = FormArray(j).Name Then

         If CurrValue = "Pass" Then
            FormArray(j).Controls(Control_Yes) = True
            FormArray(j).Controls(Control_No) = False
         ElseIf CurrValue = "Fail" Then
            FormArray(j).Controls(Control_Yes) = False
            FormArray(j).Controls(Control_No) = True 
         End If
       End If
     Next
Next
Test_Procedure1.Show '(show the first form and the for rest form the user can press on button 'next' in each form..)

End Sub

尝试替换
UserForms(FormName)。Show
FormName。Show
(不带引号)。但是如何替换呢
FormName
是一个字符串变量,它被设置为具有实际的表单名称。@AntiDrondert-它不起作用,错误消息是“无效限定符”,因为您需要加载表单才能在
UserForms
@AntiDrondert中使用。我需要在代码中更改什么才能使其起作用?thanksI无法启用“信任访问VBA项目对象模型”复选框(已被阻止),因为它不工作。这是对Set frm=UserForms.Add(FormName)的Stuck,错误信息:“参数非可选”如果您使用此答案,请删除用户函数
UserForms
。引发上述错误,因为它引用的是用户定义的函数。
Public Sub Load_Data_to_Form()
Dim T_Test As Integer
Dim CurrRaw As Integer
Dim CurrValue As String
Dim FormName As String
Dim Control_Yes As String
Dim Control_No As String
Dim FormArray As Variant 'define array which hold userforms names
FormArray = Array(Test_Procedure1, Test_Procedure2, Test_Procedure3, Test_Procedure4)

T_Test = Sheets("T_list").Range("T1").Value   'Total raws in excel table
CurrRaw = Sheets("T_list").Range("N7").Value   'current raw

For i = 1 To T_Test 'running on test parameter table
    CurrValue = Sheets("Test_Data").Range("D_Start").Offset(CurrRaw, i + 7).Value
    FormName = Sheets("T_list").Range("T_Start").Offset(i, 6).Value
    Control_Yes = Sheets("T_list").Range("T_Start").Offset(i, 4).Value
    Control_No = Sheets("T_list").Range("T_Start").Offset(i, 5).Value

    For j = 0 To 3 'Running on 4 values in the FormArray
      If FormName = FormArray(j).Name Then

         If CurrValue = "Pass" Then
            FormArray(j).Controls(Control_Yes) = True
            FormArray(j).Controls(Control_No) = False
         ElseIf CurrValue = "Fail" Then
            FormArray(j).Controls(Control_Yes) = False
            FormArray(j).Controls(Control_No) = True 
         End If
       End If
     Next
Next
Test_Procedure1.Show '(show the first form and the for rest form the user can press on button 'next' in each form..)

End Sub