分享

学以致用,ComboBox控件代替数据验证,工作表单元格添加下拉列表

 冷茶视界 2024-04-25 发布于江苏

点【关于本公众号】了解一下,欢迎关注谢谢!

快速浏览

实用案例

|日期控件||简单的收发存||收费管理系(Access改进版)|

|电子发票管理助手||电子发票登记系统(Access版)|

|文件合并||表格拆分||审计凭证抽查底稿|

|中医诊所收费系统(Excel版)||中医诊所收费系统(Access版)||银行对账单自动勾对|

|印章使用登记系统|

收费使用项目

|财务管理系统||工资薪金和年终奖个税筹划||新税法下工资表模版|

内容提要

  • ComboBox在工作表中的应用
大家好,我是冷水泡茶。
在昨天分享的最后,我们说,“动态设置单元格数据验证的方法,也可以改为使用【复合框ComboBox控件】“,今天我们不防就把“付款申请单“中的几个数据验证的下拉列表改为用ComboBox来实现。
基本思路与实现过程:
1、在工作表“付款申请单”上添加一个ComboBox控件,我们添加Active控件,不要添加表单控件。
2、模仿SetDataValidation过程,我们增加一个SetComboBox过程,当选中指定单元格时,把ComboBox的大小、位置调整为与目标单元格一致,同时给ComboBox添加List:
Sub SetComboBox(rng As Range, arr())    With Me.ComboBox1        .Clear        .Visible = True        .Left = rng.Left        .Top = rng.Top        .Height = rng.Height        .Width = rng.Width        .List = arr    End WithEnd Sub
3、设置comboBox的事件,既能选择项目录入,也可以直接输入内容:
Private Sub ComboBox1_Click()    '//点选一个项目,赋值给单元格,隐藏控件    Selection = Me.ComboBox1    Me.ComboBox1.Visible = FalseEnd Sub
Private Sub ComboBox1_Change() '//输入内容同步到目标单元格 If Me.ComboBox1 <> "" Then Selection = Me.ComboBox1 End IfEnd Sub
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) '//当我们在combobox的文本框输入内容,按回车结束输入,隐藏控件 If KeyCode = vbKeyReturn Then Me.ComboBox1.Visible = False End IfEnd Sub
4、修改Selection Change代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)    '//2024-4-24修改    On Error Resume Next    Dim iRow As Integer, iCol As Integer    Dim iWidth As Single        '//结算方式、付款单号、款项说明、内容说明单元格添加ComboBox下拉列表控件    Dim ws As Worksheet, currRow As Integer, lastRow As Integer    Dim arr(), dic As Object, dkey As String        Set dic = CreateObject("Scripting.Dictionary")    dbs = ThisWorkbook.FullName    tbl = "[数据$]"    dkey = Target.Address    currRow = Target.Row        If dkey = clsRG.结算方式.MergeArea.Address Then        sql = "select distinct 结算方式 from " & tbl & " where isnull(结算方式)=false"        dic(dkey) = 1    ElseIf dkey = clsRG.付款单号.MergeArea.Address Then        sql = "select distinct 付款单号 from " & tbl & " where isnull(付款单号)=false order by 付款单号 DESC"        dic(dkey) = 1    ElseIf dkey = clsRG.款项用途.MergeArea.Address Then        sql = "select distinct 款项用途 from " & tbl & " where isnull(款项用途)=false"        dic(dkey) = 1    ElseIf dkey = clsRG.内容说明.MergeArea.Address Then        sql = "select distinct 内容说明 from " & tbl & " where isnull(内容说明)=false"        dic(dkey) = 1    End If        If dic.exists(dkey) Then'        Target.Validation.Delete        On Error Resume Next        arr = clsDQ.getData(sql)        On Error GoTo 0        If Not IsArrEmpty(arr) Then            'Call SetDataValidation(arr, Target)            arr = Application.WorksheetFunction.Transpose(arr)            Call SetComboBox(Target, arr)        End If    Else                Me.ComboBox1.Visible = False'        Me.ComboBox1.Clear            End If        If Target.Address = clsRG.收款单位.MergeArea.Address Then        iWidth = Range("B3:H3").Width        With Me.TextBox1            .Visible = True            .Top = Target.Top + Target.Height            .Left = Target.Left            .Width = Target.Width            .Height = Target.Height            With Me.ListBox1                .Visible = True                .Top = Me.TextBox1.Top + Me.TextBox1.Height                .Left = Me.TextBox1.Left                .Width = iWidth                .ColumnCount = 3                If IsArrEmpty(arr) Then                    arr = getList                End If                iRow = UBound(arr)                iCol = UBound(arr, 2)                ReDim arrtemp(0 To iCol, 0 To iRow)                For i = 0 To iCol                    For j = 0 To iRow                        arrtemp(i, j) = arr(j, i)                    Next                Next                .List = arrtemp                .Height = 30 + (.ListCount - 1) * 12                If .Height > 100 Then                    .Height = 100                End If            End With        End With    Else        With Me.TextBox1            .Visible = False            .Text = ""        End With        Me.ListBox1.Visible = False    End IfEnd Sub
主要把跟数据验证有关的代码删除或注释掉,增加设置ComboBox控件的代码,基本逻辑没有变,就是把添加数据验证改为显示ComboBox控件。
查询数据的SQL语句也作了修改,增加了Where语句,剔除Null值,原因是数组有Null值就用不了Transpose函数。

后记

1、时间仓促,不排除有BUG,等发现再修改吧。
2、收款单位仍然是采用TextBox+ListBox的方式,没有改变,实际上也可以改为用ComboBox,主要是模糊查询不太好弄,好像分享过一个案例【用户窗体组合框动态添加下拉列表,ListView展示明细数据,TextBox录入数据更新工作表】感兴趣的朋友可以参考。
好,今天就到这里,我们下期再会!
~~~~~~End~~~~~~

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多