不想在特定页面上添加控件

时间:2011-08-02 19:43:36

标签: excel vba excel-vba

我遇到了一个不寻常的问题。 我有一套程序在不同的页面上添加一堆控件 一个添加框架

Public Sub AddFramesNP(form, pagina, nrpag)
Set cControl = form!main.Pages(nrpag).Controls.Add("Forms.Frame.1", "io" & masina, True)
        With cControl
            .Caption = "IO"
            .Width = 210
            .Height = 360
            .Top = 2
            .Left = 5
        End With
    Set cControl = form!main.Pages(nrpag).Controls.Add("Forms.Frame.1", "nio" & masina, True)
        With cControl
            .Caption = "nIO"
            .Width = 210
            .Height = 360
            .Top = 2
            .Left = 220
        End With
    Set cControl = form!main.Pages(nrpag).Controls.Add("Forms.Frame.1", "desc" & masina, True)
        With cControl
            .Caption = "Descriere"
            .Width = 210
            .Height = 360
            .Top = 2
            .Left = 435
        End With
End Sub

添加标签

Public Sub AddLabsNP(form, pagina, replicare, den, den1, den2)
Dim k, l As Integer
    If den = 1 Then
        Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lden1" & pagina, True)
            With cControl
                .Caption = den1
                .Width = 40
                .Height = 10
                .Top = 5
                .Left = 5
            End With
        Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lden1nio" & pagina, True)
            With cControl
                .Caption = den1
                .Width = 40
                .Height = 10
                .Top = 5
                .Left = 5
            End With
        End If
   If replicare = 1 Then
        Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lden2" & pagina, True)
        With cControl
            .Caption = den2
            .Width = 40
            .Height = 10
            .Top = 165
            .Left = 5
        End With
    Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lden2nio" & pagina, True)
        With cControl
            .Caption = den2
            .Width = 40
            .Height = 10
            .Top = 165
            .Left = 5
        End With
    End If
    Do While l < replicare + 1
        Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lreper" & l & pagina, True)
            With cControl
                .Caption = "Reper"
                .Width = 35
                .Height = 9
                .Top = 25 + k
                .Left = 5
            End With
        Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lsn" & l & pagina, True)
            With cControl
                .Caption = "SN"
                .Width = 35
                .Height = 9
                .Top = 25 + k
                .Left = 70
            End With
        Set cControl = form.Controls("io" & masina).Add("Forms.Label.1", "lqt" & l & pagina, True)
            With cControl
                .Caption = "Qt"
                .Width = 35
                .Height = 9
                .Top = 25 + k
                .Left = 155
            End With
        Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lrepernio" & l & pagina, True)
            With cControl
                .Caption = "Reper"
                .Width = 35
                .Height = 9
                .Top = 25 + k
                .Left = 5
            End With
        Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lsnnio" & l & pagina, True)
            With cControl
                .Caption = "SN"
                .Width = 35
                .Height = 9
                .Top = 25 + k
                .Left = 70
            End With
        Set cControl = form.Controls("nio" & masina).Add("Forms.Label.1", "lqtnio" & l & pagina, True)
            With cControl
                .Caption = "Qt"
                .Width = 35
                .Height = 9
                .Top = 25 + k
                .Left = 155
            End With
            k = k + 155
            l = l + 1
    Loop
End Sub

一个添加ComboBoxes

Public Sub AddCboxsNP(form, pagina, replicare, nrcboxs)
Dim k, l As Integer
l = 1
        Do While l < nrcboxs + 1
            Set cControl = form.Controls("io" & masina).Add("Forms.ComboBox.1", "combo" & l & pagina, True)
                With cControl
                    .Width = 60
                    .Height = 14
                    .Top = 40 + k
                    .Left = 5
                End With
            Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "sn" & l & pagina, True)
                With cControl
                    .Width = 80
                    .Height = 28
                    .Top = 40 + k
                    .Left = 70
                End With
            Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "q" & l & pagina, True)
                With cControl
                    .Width = 30
                    .Height = 14
                    .Top = 40 + k
                    .Left = 155
                End With
            Set cControl = form.Controls("nio" & masina).Add("Forms.ComboBox.1", "combo" & l & "nio" & pagina, True)
                With cControl
                    .Width = 60
                    .Height = 14
                    .Top = 40 + k
                    .Left = 5
                End With
            Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "sn" & l & "nio" & pagina, True)
                With cControl
                    .Width = 80
                    .Height = 28
                    .Top = 40 + k
                    .Left = 70
                End With
            Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "q" & l & "nio" & pagina, True)
                With cControl
                    .Width = 30
                    .Height = 14
                    .Top = 40 + k
                    .Left = 155
                End With
        If replicare = 2 Then
            Set cControl = form.Controls("io" & masina).Add("Forms.ComboBox.1", "combo" & l & "2" & pagina, True)
                With cControl
                    .Width = 60
                    .Height = 14
                    .Top = 200 + k
                    .Left = 5
                End With
            Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "sn" & l & "2" & pagina, True)
                With cControl
                    .Width = 80
                    .Height = 28
                    .Top = 200 + k
                    .Left = 70
                End With
            Set cControl = form.Controls("io" & masina).Add("Forms.TextBox.1", "q" & l & "2" & pagina, True)
                With cControl
                    .Width = 30
                    .Height = 14
                    .Top = 200 + k
                    .Left = 155
                End With
            Set cControl = form.Controls("nio" & masina).Add("Forms.ComboBox.1", "combo" & l & "2nio" & pagina, True)
                With cControl
                    .Width = 60
                    .Height = 14
                    .Top = 200 + k
                    .Left = 5
                End With
            Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "sn" & l & "2nio" & pagina, True)
                With cControl
                    .Width = 80
                    .Height = 28
                    .Top = 200 + k
                    .Left = 70
                End With
            Set cControl = form.Controls("nio" & masina).Add("Forms.TextBox.1", "q" & l & "2nio" & pagina, True)
                With cControl
                    .Width = 30
                    .Height = 14
                    .Top = 200 + k
                    .Left = 155
                End With
            End If
            k = k + 35
            l = l + 1
        Loop
End Sub

问题在于,当我想使用它们时,我不知道为什么,但它们不适用于第3页(我将它们用于第2页,它们工作正常)。我必须在这两页(2和3)中使用这些。我还有另一组包含多页的程序,这些页面可以正常工作3页。 我真的不知道问题出在哪里。我试图手动添加组件(通过代码),它工作正常。我做了一些让这个程序只运行一次的东西吗?我看不出是什么因为与其他多次工作的程序相同! 非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我知道这是一个旧线程但只是一样......:)

我以框架代码为例。请将其用作其余部分的样本:)

此示例也在MultiPage1中创建帧。

请告诉我这是否有帮助:)

Option Explicit

Private Sub CommandButton1_Click()
    AddFramesNP Me, 3, 2
End Sub

Public Sub AddFramesNP(form As UserForm, masina, nrpag)
    Dim cControl As Object

    With form
        Set cControl = .MultiPage1.Pages(nrpag).Controls.Add("Forms.Frame.1", "io" & masina)
        With cControl
            .Caption = "IO"
            .Width = 210: .Height = 360: .Top = 2
            .Left = 5
        End With

        Set cControl = .MultiPage1.Pages(nrpag).Controls.Add("Forms.Frame.1", "nio" & masina)
        With cControl
            .Caption = "nIO"
            .Width = 210: .Height = 360: .Top = 2
            .Left = 220
        End With

        Set cControl = .MultiPage1.Pages(nrpag).Controls.Add("Forms.Frame.1", "desc" & masina)
        With cControl
            .Caption = "Descriere"
            .Width = 210: .Height = 360: .Top = 2
            .Left = 435
        End With
    End With
End Sub