禁用或隐藏组合框VB​​A Excel中的选项 [英] Disable or hide options in combo box VBA Excel

查看:87
本文介绍了禁用或隐藏组合框VB​​A Excel中的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel用户窗体中有一个由用户组类型组成的组合框.根据用户访问级别,我希望禁用某些Option \ item或使其不可见.我不想使用Removeitem,,因为我每次都必须重新填充列表!

I have a combo box in a Excel Userform that consist of User Group Types. Depending on the user access level, I would like to have some Option\item disable or not visible. I don't want to use Removeitem, Because I would have to repopulate the list every time!

sub ComboBox_Enter() 

accessLvl = 1

ComboBox.AddItem "0-Show"
ComboBox.AddItem "1-Hide or disable"
ComboBox.AddItem "2-Show"
ComboBox.AddItem "3-Show"

For i = 0 To 3
      if accessLvl = 1 Then ComboBox.List(1).Hidden = True ' This not does work!! ''
Next

End sub

我只希望将其禁用\变灰或使其不可见,但仍在组合框"列表中!*

I just want it to be disabled\grayed out or not visible but still in the Combobox list!*

推荐答案

AFAIK,您不能这样做,但是有替代方法.用户将无法选择某些项目(无论您指定哪个项目),即使它们是可见的且未被禁用.

AFAIK, you can't do that but there is an alternative. The user will not be able to select certain items (whichever you specify) even though it will be visible and not disabled.

为此,请尝试此代码

Dim boolC As Boolean

'~~> Add Sample data
Private Sub UserForm_Initialize()
    ComboBox1.AddItem "Please Choose Again"

    For i = 1 To 10
        ComboBox1.AddItem i
    Next i
End Sub

'~~> This will not let the user select items in 2nd
'~~> 3rd and 4th items
Private Sub ComboBox1_Change()
    If Not boolC Then
        boolC = True
        Select Case ComboBox1.ListIndex
            Case 1, 2, 3: ComboBox1.ListIndex = 0
        End Select
        boolC = False
    End If
End Sub

屏幕截图

假设您的表单在表单启动时看起来像这样.

Let's say your form looks like this on form start up.

选择第二,第三或第四项后,您将得到请再次选择

The moment you select the 2nd ,3rd or the 4th item, you will get Please Choose Again

这篇关于禁用或隐藏组合框VB​​A Excel中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆