自动填充组合框与主子参数

时间:2017-01-10 14:17:08

标签: excel excel-vba combobox vba

我正在使用userforms对我的工作簿执行不同的操作。 到目前为止,我能够使用一些代码,但是将我的工作复杂化导致死胡同。

表单代码中的

Public c As Integer, lf As Integer, ld As Integer

Sub main()

    Dim tri1 As Crit1, col1 As ColSel
    Set col1 = New ColSel

    'sheet specific parameters
    ld = 8
    lf = 128

    'Application.ScreenUpdating = False
    Load col1
    col1.Show
    c = col1.c

    Set tri1 = New Crit1 'The Problem is here
    tri1.c = c
    tri1.ld = ld
    tri1.lf = lf
    tri1.Show

End Sub

ColSel 是一个将从用户那里获得c的用户表单

Option Explicit

Private m_c

Property Get c()
    c = m_c
End Property

Private Sub UserForm_Initialize()
Me.txt_C1 = "B"
End Sub

Private Sub CmdOK_click()

'validation
    If Me.txt_C1 = "" Then
        MsgBox "please enter a column letter", Title:="Erreur"
    End If
    If IsNumeric(Me.txt_C1) Then
        MsgBox "Only enter letters", Title:="Erreur"
    End If
'transformation en numéro de colonne
m_c = Range(Me.txt_C1.value & "1000").Column

Me.Hide

End Sub


Private Sub CmdCancel_Click()
Unload Me
End Sub

Crit1

Option Explicit

Dim mlf As Integer, mld As Integer, mc As Integer

Property Let lf(nlf As Integer)
    mlf = nlf
End Property

Property Let ld(nld As Integer)
    mld = nld
End Property

Property Let c(nc As Integer)
    mc = nc
End Property


Private Sub UserForm_Initialize()

    Dim rng As Range, r As Range

    Set rng = Range(Cells(mld, mc), Cells(mlf, mc))
    For Each r In rng
        addunique r.value 
        'specific sub ensuring only one of each value in combobx
    Next r
End Sub


Sub addunique(value As Variant)

    Dim i As Integer, sortie As Boolean
    Dim inList As Boolean

    inList = False
    If value <> vide Then
        With Me.ComboBox1
            For i = 0 To Me.ComboBox1.ListCount - 1
                If Me.ComboBox1.List(i) = value Then
                    inList = True
                    Exit For
                End If
            Next i

            If Not inList Then
                i = .ListCount - 1
                sortie = False
                While i >= 0 And sortie = False
                    If StrComp(.List(i), value, vbTextCompare) = 1 Then
                        i = i - 1
                    Else: sortie = True
                    End If
                Wend
                .AddItem value, i + 1
            End If
        End With
    End If

End Sub


Sub CmdOK_click()

    For i = mld To mlf Step 2
        If Cells(i, mc) = Me.ComboBox1 Then
            Rows(i & ":" & i + 1).Hidden = False
        Else:
            Rows(i & ":" & i + 1).Hidden = True
        End If
    Next i
    Unload ColSel
    Unload Me

End Sub


Sub CmdCnl_click()
    Unload ColSel
    Unload Me
End Sub

正如您所看到的,在我的第二个用户表单的初始化部分,我需要(对于组合框自动完成功能,可以访问clfld 我的问题是,在我到达需要它之前,初始化不允许我实现这些值。

我试图通过公共变量但也没有成功。

1 个答案:

答案 0 :(得分:0)

为了解决您的错误,因为您需要在init进程之前获取clfld的值,您需要确保它们具有唯一的变量名称(目前在User_Forms代码中有完全相同的变量)。下一步是访问 Crit1 Public代码中的这些Private Sub UserForm_Initialize()变量。

主要模块代码:

Option Explicit

Public Pub_c As Integer, Pub_lf As Integer, Pub_ld As Integer ' < -modified the names of the Public variables

Sub main()

    Dim tri1 As Crit1
    Dim col1 As ColSel1

    Set col1 = New ColSel1

    'sheet specific parameters
    Pub_ld = 8
    Pub_lf = 128

    'Application.ScreenUpdating = False
    Load col1
    col1.Show
    Pub_c = col1.c ' <-- modifed this line

    Set tri1 = New Crit1
    tri1.Show

End Sub

Crit1 代码(仅修改UserForm_Initialize

Private Sub UserForm_Initialize()

    Dim rng As Range, r As Range

    ' added this part : modify properties according to values of Public variabels
    Me.ld = Pub_ld
    Me.lf = Pub_lf
    Me.c = Pub_c

    Set rng = Range(Cells(mld, mc), Cells(mlf, mc))
    For Each r In rng
        addunique r.value
        'specific sub ensuring only one of each value in combobx
    Next r

End Sub