You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ComboBox筛选ListBox时出现错误的问题求助

使用ComboBox筛选ListBox时出现错误的问题求助

大家好,我最近在用Excel VBA做用户表单时碰到了个棘手的问题,想请各位帮忙看看哪里出了问题。

我做了一个ListBox用来展示数据库工作表里的所有记录,现在想用一个ComboBox来筛选特定区域的记录——区域信息存在数据库的B列里。我已经把所有区域选项都添加到ComboBox里了,但每次选择某个区域、想要筛选出对应记录的时候,程序就会报错。

下面是我的代码:

初始化ComboBox的代码

Private Sub UserForm_Initialize()
    
    Dim areas() As String
    areas = Split("Assy, PPW, Coils, THS, THP, M/A, Machine Center, Tool Room, Maintenance", ", ")
    
    Dim area As Variant
    For Each area In areas
        cmbAreaFilter.AddItem area
        
    Next area
    
End Sub

筛选功能的代码(执行到此处时出错)

Private Sub cmbAreaFilter_Change()
    
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Database")
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).row
    
    Dim i As Long
    For i = 2 To lastRow
        If ws.Cells(i, "B").Value = cmbAreaFilter.Value Then
            With frmOpenRecords.ls...

我反复检查了好几次都没找到问题所在,有没有朋友能帮我排查一下?

备注:内容来源于stack exchange,提问作者Jorge Omar David Gonzalez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 15:33:05