将数据加载到变量用户表单和控件中

问题描述 投票:0回答:3

我有一个代码在excel表(T_List)中的表上运行。代码的目的是将数据从tabl加载到用户表单控件。用户表单和控件是可验证的。代码在打开时会卡住: 用户窗体(窗体名称).Show 并且错误按摩是“类型不匹配”:

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

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

If CurrValue = "Pass" Then
   UserForms(FormName).Show     '---> **the code stuck here**
   UserForms(FormName).Controls(Control_Yes) = True
   UserForms(FormName).Controls(Control_No) = False
ElseIf CurrValue = "Fail" Then
   UserForms(FormName).Show
   UserForms(FormName).Controls(Control_Yes) = False
   UserForms(FormName).Controls(Control_No) = True 
End If
Next
End Sub

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

vba excel-vba
3个回答
0
投票

这个对我有用 - 用于引用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

0
投票

UserForms.Add方法 - 没有附加功能。

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

0
投票

由于我的组织策略阻止了“信任访问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
© www.soinside.com 2019 - 2024. All rights reserved.