Excel vba:如果Listbox选择值=2,则显示文本框4,否则隐藏文本框

Excel vba:如果Listbox选择值=2,则显示文本框4,否则隐藏文本框,excel,vba,listbox,Excel,Vba,Listbox,我有一个列表框,其中包含一个名为数据库OUtypes的表中的值。价值观是: 单分裂 多分裂 多城市 我想要一个“multiplit”(文本框),当在列表框中选择值2时显示,当选择其他值时隐藏 我该怎么做 Option Explicit Private Sub CommandButton3_Click() Unload Me Menu.Show End Sub Private Sub userform_activate() Application.DisplayAlerts = False D

我有一个列表框,其中包含一个名为
数据库OUtypes
的表中的值。价值观是:

  • 单分裂
  • 多分裂
  • 多城市
  • 我想要一个“multiplit”(文本框),当在列表框中选择值
    2
    时显示,当选择其他值时隐藏

    我该怎么做

    Option Explicit
    
    Private Sub CommandButton3_Click()
    Unload Me
    Menu.Show
    End Sub
    
    Private Sub userform_activate()
    Application.DisplayAlerts = False
    Dim cell As Range
    With Worksheets("Database Bedrijf")
    For Each cell In .Range("B1:B1000" & .cells(Rows.Count, 3).End(xlUp).Row)
    If Not IsEmpty(cell) Then Bedrijf.AddItem cell.Value
    Next cell
    End With
    With Worksheets("Database Freon")
    For Each cell In .Range("B1:B1000" & .cells(Rows.Count, 3).End(xlUp).Row)
    If Not IsEmpty(cell) Then Freontype.AddItem cell.Value
    Next cell
    End With
    With Worksheets("Database OUtypes")
    For Each cell In .Range("B2:B1000" & .cells(Rows.Count, 3).End(xlUp).Row)
    If Not IsEmpty(cell) Then OUtypes.AddItem cell.Value
    Next cell
    End With
    '''This declares the data type of the variable "LstRw'
    Dim LstRw As Long
    '''This defines what the variable "LstRw' is to refer to. _
       (The row number of the last used cell in column A.)
    LstRw = cells(Rows.Count, "A").End(xlUp).Row
    '''This tells the textbox named ID to equal the value of the last used cell in Col.A after adding 1 to it.
    ID.caption = cells(LstRw, "A").Value + 1
    End Sub
    
    
    Private Sub Freontypes_Change()
    
        gwp.Text = Application.VLookup(Freontype.Value, Worksheets("Database Freon").Range("B2:C1000"), 2, False)
    
    End Sub
    
    Private Sub Bedrijf_Change()
    Dim RowMax As Integer
    Dim wsh As Worksheet
    Dim countExit As Integer
    Dim CellCombo2 As String
    Dim i As Integer
    
    Set wsh = ThisWorkbook.Sheets("Database Klant")
    RowMax = wsh.cells(Rows.Count, "B").End(xlUp).Row
    'find last row of sheet in column A
    
    Klant.Clear
    'clear all value of comboBox2
    
    With Klant
        For i = 2 To RowMax
            If wsh.cells(i, "B").Value = Bedrijf.Text Then
            'Just show value of mapping with column A
            .AddItem wsh.cells(i, "C").Value
            Else
            End If
        Next i
    End With
    End Sub
    Private Sub Freoninhoud_Change()
        If Freoninhoud.Text = "" Then
            MsgBox "Vul iets in"
            Exit Sub
        Else
         Co2.Text = CDbl(Replace(Me.Freoninhoud.Text, ".", ",")) * gwp.Text
        End If
    End Sub
    
    Private Sub Userform_Initialize()
        Status.AddItem "Goed"
        Status.AddItem "Slecht"
        Status.AddItem "Defect"
    
    Dim RowMax As Integer
    Dim wsh As Worksheet
    Dim countExit As Integer
    Dim CellCombo1 As String
    Dim i As Integer
    Dim j As Integer
    
    Set wsh = ThisWorkbook.Sheets("Database Bedrijf")
    RowMax = wsh.cells(Rows.Count, "B").End(xlUp).Row
    'find last row of sheet in column A
    
    Bedrijf.Clear
    'clear all value of comboBox1
    
    With Bedrijf
    
        For i = 2 To RowMax
        'Run each row of column A
    
        countExit = 0
        CellCombo1 = wsh.cells(i, "B").Value
    
                For j = i To 2 Step -1
                'just show value not duplicate
    
                If CellCombo1 = wsh.cells(j, "A").Value Then
                countExit = countExit + 1
                End If
                Next j
            If countExit = 0 Then
    
            ElseIf countExit > 1 Then
                Else
                .AddItem CellCombo1
                End If
        Next i
    
    End With
    End Sub
    
    
    Private Sub CommandButton1_Click()
    Dim lRow As Long
        Dim ws As Worksheet
        Set ws = Worksheets("Database OU")
        lRow = ws.cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            With ws
            .cells(lRow, 1).Value = ID.caption
            .cells(lRow, 2).Value = Bedrijf.Value
            .cells(lRow, 3).Value = Klant.Value
            .cells(lRow, 4).Value = Ruimte.Value
            .cells(lRow, 5).Value = Merk.Value
            .cells(lRow, 6).Value = Types.Value
            .cells(lRow, 7).Value = Multisplit.Value
            .cells(lRow, 8).Value = Model.Value
            .cells(lRow, 9).Value = Serienummer.Value
            .cells(lRow, 10).Value = Bouwjaar.Value
            .cells(lRow, 11).Value = Afvoer.Value
            .cells(lRow, 12).Value = Freontype.Value
            .cells(lRow, 13).Value = Freoninhoud.Value
            .cells(lRow, 14).Value = Co2.Text
            .cells(lRow, 15).Value = Installatienummer.Value
            .cells(lRow, 16).Value = Adres.Value
            .cells(lRow, 17).Value = Status.Value
        End With
        Unload Me
        Menu.Show
    End Sub
    Private Sub CommandButton2_Click()
    Dim lRow As Long
        Dim ws As Worksheet
        Set ws = Worksheets("Database OU")
        lRow = ws.cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            With ws
            .cells(lRow, 1).Value = ID.caption
            .cells(lRow, 2).Value = Bedrijf.Value
            .cells(lRow, 3).Value = Klant.Value
            .cells(lRow, 4).Value = Ruimte.Value
            .cells(lRow, 5).Value = Merk.Value
            .cells(lRow, 6).Value = Types.Value
            .cells(lRow, 7).Value = Multisplit.Value
            .cells(lRow, 8).Value = Model.Value
            .cells(lRow, 9).Value = Serienummer.Value
            .cells(lRow, 10).Value = Bouwjaar.Value
            .cells(lRow, 11).Value = Afvoer.Value
            .cells(lRow, 12).Value = Freontype.Value
            .cells(lRow, 13).Value = Freoninhoud.Value
            .cells(lRow, 14).Value = Co2.Text
            .cells(lRow, 15).Value = Installatienummer.Value
            .cells(lRow, 16).Value = Adres.Value
            .cells(lRow, 17).Value = Status.Value
        End With
        Unload Me
        Outoevoegen.Show
    End Sub
    
    
    Private Sub Userform_QueryClose(Cancel As Integer, closemode As Integer)
    If closemode = vbFormControlMenu Then
        MsgBox "Sorry gebruik de Sluiten knop"
        Cancel = True
    End If
    End Sub
    
    找到了

    Private Sub OUtypes_Change()
    If OUtypes = "Multi Split" Then
    Label18.Visible = True
    Multisplit.Visible = True
    Else
    Label18.Visible = False
    Multisplit.Visible = False
    End If
    End Sub
    
    编辑:可以使用布尔值将其合并为更清晰一点:

    Private Sub OUtypes_Change()
    
        visible = (OUtypes = "Multi Split") ' Boolean expression
        Label18.Visible = visible
        Multisplit.Visible = visible
    
    End Sub
    

    欢迎光临!这是一个程序员编写自己的代码并在试图自己解决某个特定问题后与之共享问题的网站。如果,之后,你有一个特定的问题,请分享你的帖子和一些背景信息。一些好的阅读材料让您开始:“以及提示和抱歉完全忘记了代码我建议对您的答案进行修订,使代码更清晰,并利用布尔表达式:)