首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在VBA中引用来自另一个用户表单的过程生成标签中的值

在VBA中引用来自另一个用户表单的过程生成标签中的值
EN

Stack Overflow用户
提问于 2019-01-18 05:32:24
回答 1查看 223关注 0票数 0

我正在创建一个UI,它可以插入、删除和显示来自多个数据集的信息。用户单击命令按钮启动程序,输入一些数据,userform1使用这些数据创建userform2。在使用userform2时,我希望用户能够根据在userform2上生成的文本框中输入的信息来更新用户表单和excel工作表上显示的信息。我遇到的问题是,当我尝试在Userform2中引用从UserForm1为UserForm2生成的txtbox和标签时,它找不到它们。我也使用了一个for循环来命名它们,所以我认为它应该和Rag2.caption或UserForm2!Rag2.Caption一样简单。UserForm1:

代码语言:javascript
运行
复制
Private Sub CommandButton1_Click()

UserForm1.Show

End Sub

Private Sub CANCELBUTT_Click()

Unload Me

End Sub

Public Sub InsertBUTT_Click()
Dim check As Range
If LINBOX.Value <> "" And NOMBOX.Value <> "" Then
    Set check = Columns("A:A").Find(What:=LINBOX.Value, After:=Range("A1"), _
                LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If check Is Nothing Then
        Dim BlankRow As Long
        BlankRow = Range("A65536").End(xlUp).Row + 1
        Cells(BlankRow, 1).Value = LINBOX.Value
        Cells(BlankRow, 2).Value = NOMBOX.Value
    End If
Else
    MsgBox "Both LIN and Nomenclature are required to insert a new LIN"
End If

Unload Me
UserForm1.Show

End Sub

Private Sub LINBOX_DropButtonClick()

Dim cl As Range

With ActiveSheet
    For Each cl In Range([A3].CurrentRegion.Columns(1).Address)
        If cl.Value <> "" Then
             With LINBOX
                .AddItem cl.Value
             End With
        End If
    Next cl

End With

End Sub
Private Sub LINBOX_Change()
Dim Rng As Range
Dim cat As Integer
    ' Create a new Combo Box for the overhead categories
    UserForm1.Controls.Add "Forms.ComboBox.1", "CATBOX", True
    UserForm1!CATBOX.Visible = False
        With UserForm1!CATBOX
            .Height = 20
            .Width = 150
            .Left = 100
            .Top = 40
        End With

    If LINBOX.Value <> "" Then

    ' Find the position of the LIN and display the corresponding Nomenclature
        Set Rng = Columns("A:A").Find(What:=LINBOX.Value, After:=Range("A1"), _
                LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Rng Is Nothing Then
    'Do nothing
        Else
            NOMBOX = ActiveSheet.Cells(Rng.Row, Rng.Column + 1).Value

            UserForm1!CATBOX.Visible = True

    ' Fill the combo box with the Category Titles
            With ActiveSheet
                For cat = 1 To 999
                    col = ActiveSheet.Cells(1, cat).Value
                    If col <> "" Then
                        With UserForm1!CATBOX
                            .AddItem col
                        End With
                    End If
                Next cat
            End With
        End If
    End If
End Sub


Private Sub OKBUTT_Click()
Dim Rng As Range, SubRng As Range, subVal As Range, Rngr As Range
Dim Rw As Long, ColSt As Long, ColEnd As Long, i As Long, ScatNo As Long
Dim Rag As Object, Rag2 As Object, Rag3 As Object
 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ' Label the new userform
    If (UserForm1!LINBOX.Value = "") Then
        MsgBox "Your Query wasn't found at this time"
    ElseIf (UserForm1!NOMBOX.Value = "") Then
        MsgBox "Your Query wasn't found at this time"
    ElseIf (UserForm1!CATBOX.Value = "") Then
        MsgBox "Your Query wasn't found at this time"
    ElseIf (UserForm1!LINBOX.Value = "") And (UserForm1!NOMBOX.Value = "") And (UserForm1!CATBOX.Value = "") Then
        MsgBox "Your Query wasn't found at this time"
    Else
        Set LINB = UserForm2.Controls.Add("Forms.Label.1", "LINB", True)
        With LINB
            .Caption = LINBOX.Value
            .Left = 10
            .Width = 50
            .Top = 5
        End With

        Set NOMB = UserForm2.Controls.Add("Forms.Label.1", "NOMB", True)
        With NOMB
            .Caption = NOMBOX.Value
            .Left = 10
            .Width = 200
            .Top = 15
        End With


        Set CATB = UserForm2.Controls.Add("Forms.Label.1", "CATB", True)
        With CATB
            .Caption = UserForm1!CATBOX.Value
            .Left = 400
            .Width = 200
            .Top = 5
        End With
 ''''''''''''''''''''''''''''''Merged Columns Start & End''''''''''''''''''''''''''''''''''''''''''''''''''
        With ActiveSheet

            '' Find the Category in the first row
            Set Rng = .Rows(1).Find(What:=UserForm1!CATBOX.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            '' If the category is not found, then it won't go through the code
            If Rng Is Nothing Then Exit Sub
            '' Determine the range of the merged columns
            Set Rng = Rng.MergeArea
            Set rngStart = Rng.Cells(1, 1)
            Set rngEnd = Rng.Cells(Rng.Rows.Count, Rng.Columns.Count)
            Rw = Rng.Row + Rng.Rows.Count
            ColSt = Rng.Column
            ColEnd = Rng.Column + Rng.Columns.Count - 1
            Debug.Print Rw, ColSt, ColEnd
            '' Use the merged column range to determine the length of the parser, then print the value
            ''      to the new userform
    '''''''''''''''''''''''''''''''''''''''SUBCAT TITLE''''''''''''''''''''''''
            Set Rng = .Range(.Cells(Rw, ColSt), .Cells(Rw, ColEnd))
            ScatNo = 0
            '' Establish a row counter
            a = 0
               For Each SubRng In Rng
                    If SubRng.Value <> "" Then
                        ScatNo = ScatNo + 1
                        '' Create a label an give it the subcatagory value
                        Set Rag = UserForm2.Controls.Add("Forms.Label.1", "Scat" & ScatNo)
                        Rag.Caption = SubRng.Value & ":"
                        '' Check if ScatNo is part of the new row or not, anything >7 is, anything <7 is not
                        If a > 0 Then
                        '' Establish when to create a new row (every 7th data set)
                            If (ScatNo Mod 7) = 0 Then
                                Rag.Left = 30
                                Rag.Width = 50
                                Rag.Top = 40 + (a * 20)
                        '' Make the following ScatNos part of the same row
                            Else
                                Rag.Top = 40 + (a * 20)
                                Rag.Left = ((ScatNo + 1) - (a * 7)) * 125 - 85
                                Rag.Width = 50
                                If ((ScatNo + 1) Mod 7) = 0 Then
                                    a = a + 1
                                End If
                            End If
                        '' If ScatNo is less than 7
                        ElseIf a = 0 Then
                            Rag.Left = ScatNo * 90
                            If Rag.Left = 90 Then
                                Rag.Left = 30
                                Rag.Top = 40
                                Rag.Width = 50
                            Else
                                Rag.Left = ScatNo * 125 - 85
                                Rag.Top = 40
                                Rag.Width = 50
                                If ((ScatNo + 1) Mod 7) = 0 Then
                                    a = a + 1
                                End If
                            End If
                        End If
                    End If
               Next
    ''''''''''''''''''''''''''''''''''''''''''SUBCAT Values & TXTBOX'''''''''''''''''''''''''''''
               Set Rngr = Columns("A:A").Find(What:=LINBOX.Value, After:=Range("A1"), _
                    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
               If (Rngr Is Nothing) Then
                   MsgBox "Your Query wasn't found at this time"
               Else
                   Set subVal = .Range(.Cells(Rngr.Row, ColSt), .Cells(Rngr.Row, ColEnd))
                   scat = 0
                   '' Establish a row counter
                   a = 0
                   For Each vale In subVal
                         scat = scat + 1
                         Set Rag2 = UserForm2.Controls.Add("Forms.Label.1", "ScatV" & ScatNo)
                         Set Rag3 = UserForm2.Controls.Add("Forms.TextBox.1", "ScatUp" & ScatNo)
                         Rag2.Caption = vale.Value
                         '' Check if ScatNo is part of the new row or not, anything >7 is anything <7 is not
                         If a > 0 Then
                         '' Establish when to create a new row (every 7th data set)
                             If (scat Mod 7) = 0 Then
                                 Rag2.BackColor = RGB(200, 200, 200)
                                 Rag2.Left = 70
                                 Rag3.Left = 90
                                 Rag2.Width = 50
                                 Rag3.Width = 50
                                 Rag2.Top = 40 + (a * 20)
                                 Rag3.Top = 40 + (a * 20)
                         '' Make the following Scats part of the same row
                             Else
                                 Rag2.BackColor = RGB(200, 200, 200)
                                 Rag2.Top = 40 + (a * 20)
                                 Rag3.Top = 40 + (a * 20)
                                 Rag2.Left = ((scat + 1) - (a * 7)) * 125 - 35
                                 Rag3.Left = ((scat + 1) - (a * 7)) * 125 - 15
                                 Rag2.Width = 50
                                 Rag3.Width = 50
                                 If ((scat + 1) Mod 7) = 0 Then
                                    a = a + 1
                                End If
                             End If
                         '' If Scat is less than 7
                         ElseIf a = 0 Then
                             Rag2.Left = scat * 125 - 25
                             Rag2.BackColor = RGB(200, 200, 200)
                             If Rag2.Left = 100 Then
                                 Rag2.Left = 70
                                 Rag3.Left = 90
                                 Rag2.Top = 40
                                 Rag3.Top = 40
                                 Rag2.Width = 50
                                 Rag3.Width = 50
                             Else
                                 Rag2.Left = scat * 125 - 35
                                 Rag3.Left = scat * 125 - 15
                                 Rag2.Top = 40
                                 Rag3.Top = 40
                                 Rag2.Width = 50
                                 Rag3.Width = 50
                                 If ((scat + 1) Mod 7) = 0 Then
                                    a = a + 1
                                End If
                             End If
                         End If
                Next
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            UserForm2.Show
        End If
End With
End If
End Sub

UserForm2:

代码语言:javascript
运行
复制
Private Sub CANCELBUTT_Click()

Unload Me

End Sub

Private Sub DELETEBUTT_Click()

Dim RngD As Range
Set RngD = Columns("A:A").Find(What:=UserForm1!LINBOX.Value, After:=Range("A1"), _
                LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Rows(RngD.Row).EntireRow.Delete
Unload Me

End Sub

Private Sub InsertBUTT_Click()
Dim Rng As Range, SubRng As Range, subVal As Range, Rngr As Range
Dim Rw As Long, ColSt As Long, ColEnd As Long, i As Long, ScatNo As Long
Dim Rag As Object, Rag2 As Object, Rag3 As Object
 ''''''''''''''''''''''''''''''Merged Columns Start & End''''''''''''''''''''''''''''''''''''''''''''''''''
With ActiveSheet

    '' Find the Category in the first row
    Set Rng = .Rows(1).Find(What:=UserForm1!CATBOX.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    '' If the category is not found, then it won't go through the code
    If Rng Is Nothing Then Exit Sub
    '' Determine the range of the merged columns
    Set Rng = Rng.MergeArea
    Set rngStart = Rng.Cells(1, 1)
    Set rngEnd = Rng.Cells(Rng.Rows.Count, Rng.Columns.Count)
    Rw = Rng.Row + Rng.Rows.Count
    ColSt = Rng.Column
    ColEnd = Rng.Column + Rng.Columns.Count - 1
    Debug.Print Rw, ColSt, ColEnd
    '' Use the merged column range to determine the length of the parser, then print the value
    ''      to the new userform
''''''''''''''''''''''''''''''''''''''''''SUBCAT Values & TXTBOX'''''''''''''''''''''''''''''
       Set Rngr = Columns("A:A").Find(What:=UserForm1!LINBOX.Value, After:=Range("A1"), _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
       If (Rngr Is Nothing) Then
           MsgBox "Your Query wasn't found at this time"
       Else
           Set subVal = .Range(.Cells(Rngr.Row, ColSt), .Cells(Rngr.Row, ColEnd))
           scat = 0
           '' Establish a row counter
           a = 0
           For Each vale In subVal
                 scat = scat + 1
                 If UserForm2!Rag3.Value <> "" Then
                    vale.Value = UserForm2!Rag3.Value
                    UserForm2!Rag2.Caption = UserForm2!Rag3.Value
                    UserForm2!Rag3.Value = ""
                 Else
                    UserForm2!Rag2.Caption = vale.Value
                 End If
                 '' Check if ScatNo is part of the new row or not, anything >7 is anything <7 is not
                 If a > 0 Then
                 '' Establish when to create a new row (every 7th data set)
                     If (scat Mod 7) = 0 Then
                         Rag2.BackColor = RGB(200, 200, 200)
                         Rag2.Left = 70
                         Rag3.Left = 90
                         Rag2.Width = 50
                         Rag3.Width = 50
                         Rag2.Top = 40 + (a * 20)
                         Rag3.Top = 40 + (a * 20)
                 '' Make the following Scats part of the same row
                     Else
                         Rag2.BackColor = RGB(200, 200, 200)
                         Rag2.Top = 40 + (a * 20)
                         Rag3.Top = 40 + (a * 20)
                         Rag2.Left = ((scat + 1) - (a * 7)) * 125 - 35
                         Rag3.Left = ((scat + 1) - (a * 7)) * 125 - 15
                         Rag2.Width = 50
                         Rag3.Width = 50
                         If ((scat + 1) Mod 7) = 0 Then
                            a = a + 1
                        End If
                     End If
                 '' If Scat is less than 7
                 ElseIf a = 0 Then
                     Rag2.Left = scat * 125 - 25
                     Rag2.BackColor = RGB(200, 200, 200)
                     If Rag2.Left = 100 Then
                         Rag2.Left = 70
                         Rag3.Left = 90
                         Rag2.Top = 40
                         Rag3.Top = 40
                         Rag2.Width = 50
                         Rag3.Width = 50
                     Else
                         Rag2.Left = scat * 125 - 35
                         Rag3.Left = scat * 125 - 15
                         Rag2.Top = 40
                         Rag3.Top = 40
                         Rag2.Width = 50
                         Rag3.Width = 50
                         If ((scat + 1) Mod 7) = 0 Then
                            a = a + 1
                        End If
                     End If
                 End If
        Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End If
End With
End Sub

显然,我对UserForm2感到失望,因为我提供的代码显示我试图简单地重写已经存在的标签和txtboxes,但即使这样也不起作用。

Example Data

上面的链接是我正在尝试处理的数据的一个例子(我还没有嵌入图片的代表,对不起)。感谢所有的帮助!

EN

回答 1

Stack Overflow用户

发布于 2019-01-18 09:25:41

您似乎试图通过单击工作表上的按钮来创建UserForm1,并通过单击UF1上的按钮来创建UserForm2。然后,您希望UF2更新UF1,并让UF1更新工作表。您的代码不能做到这一点,因为没有对象UF1,也没有对象UF2,实际上也没有工作表的对象。例如,

代码语言:javascript
运行
复制
Dim Ws As Worksheet
Dim Uf1 as UserForm1
Dim Uf2 As UserForm2

Set Ws = ActiveSheet
Set Uf1 = New UserForm1
Set Uf2 = New UserForm2        ' actually to be declared only later in the code

现在您可以使用Uf1.Show来显示UserForm1,使用Uf2.Hide来隐藏它。请记住,Unload Uf1会将表单从内存中删除,而当表单被隐藏时,情况并非如此。您可以在隐藏它之后再次显示它,也可以卸载它并创建它的一个新实例,具体取决于您想要执行的操作。

在使用Set New命令创建它之后,在显示之前,在隐藏它之后,但在卸载之前,您可以访问每个用户表单对象的所有控件,如Uf1.TextBox1.Text以读取或写入。Ws.Cells(1,1).Value = Uf1.TextBox1.Text会将文本框的内容传输到工作表的单元格A1。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54244605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档