在Excel单元格中创建ComboBox

时间:2018-02-10 16:37:56

标签: excel vba excel-vba combobox

我想创建一个Select或Drop-down List或ComboBox(无论你想叫它什么)。

我不想用UserForm这样做,因为它在单元格上显示一个窗口,我希望它在单元格中。 示例(见图片)

enter image description here

我想在组合框中选择 PERRO 时,单元格显示数字1,如果 GATO ,则单元格显示数字2,如果 VACA 单元格显示数字3。

2 个答案:

答案 0 :(得分:1)

你有三个选择

  1. 使用数据验证(DATA> DATA TOOLS> DATA VALIDATION),如图所示,但您必须将其与VLOOKUP功能相结合(有点痛苦)

  2. 右键单击并附加Combobox表单控件(DEVELOPER> CONTROLS> INSERT)并转到格式控件。从这里,您可以以数字格式分配源列表和结果单元格。我的意思是如果你选择Perro,因为它是第一个元素,结果单元格给出值1.这对我来说是最好的方式。

  3. enter image description here

    1. 最后,您可以使用Combobox ActiveX控件(DEVELOPER> CONTROLS> INSERT),您可以使用AddItem方法添加属性窗口或VBA脚本中的元素
    2. 例如:

      With ComboBox
        .AddItem "perro"
        .AddItem "gato"
        .AddItem "vaca"
        .AddItem "cerdo"
      End With
      

      您可以使用条件

      创建结果单元格
      Select Case Combobox.value
        Case "perro": range("a1")=1
        Case "gato": range("a1")=2
        Case "vaca": range("a1")=3
        Case "cerdo": range("a1")=4
      End Select
      

      这对初学者来说很容易。

答案 1 :(得分:0)

除了QHarr在评论中提供的链接的解决方案之外,您可以尝试完全VBA解决方案,每次用户选择想要的单元格时,代码都会显示验证列表

这样做你必须:

  • 在每个单元格选择中删除并放回验证规则
  • 使用列表中的索引替换列表项目选择

因此您需要处理Worksheet_SelectionChange()Worksheet_Change()事件处理程序

将以下代码放在工作表代码窗格

Option Explicit

Dim animals As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$2" Then ' if selected cell is the wanted one (change "$B$2" to any wanted address
        Dim element As Variant
        Dim position As Long
        For Each element In animals 'loop through validation list
            position = position + 1 'update index position
            If element = Target.Value Then 'if current loop element matches cell content
                Application.EnableEvents = False 'prevent subsequent sheet change (deleting and writing cell content) fire this event again and start an infinite loop
                Target.Validation.Delete 'remove data validation
                Target.Value = position ' write the element index position
                Application.EnableEvents = True 'restore proper event handling
                Exit For
            End If
        Next
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$B$2" Then ' if selected cell is the wanted one (change "$B$2" to any wanted address
        With Target
            If hasValidation(.Cells) Then
                .ClearContents 'clear any previous cell content
            Else
                animals = Array("Perro", "Gato", "Vaca", "Cerdo") 'set the validation list
                .ClearContents 'clear any previous cell content
                With .Validation 'set validation rules
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(animals, ",")
                    .IgnoreBlank = True
                    .InCellDropdown = True
                    .InputTitle = ""
                    .ErrorTitle = ""
                    .InputMessage = ""
                    .ErrorMessage = ""
                    .ShowInput = True
                    .ShowError = True
                End With
            End If
        End With
    End If
End Sub


Function hasValidation(rng As Range) As Boolean
    On Error Resume Next
    hasValidation = rng.SpecialCells(xlCellTypeSameValidation).Cells.Count = 1
End Function