AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79106361
Accepted
Shiela
Shiela
Asked: 2024-10-20 12:23:27 +0800 CST2024-10-20 12:23:27 +0800 CST 2024-10-20 12:23:27 +0800 CST

带日期条件/过滤器的 VBA 案例

  • 772

使用以前的文章,我添加了:

  • 新列为名称和日期
  • 新增 6 个标签(丹的每日和每月计数、丽莎的每日和每月计数、每日总计数和每月计数)
  • 新建列表框2

我遇到一个问题,在 Listbox1 的结果是今天的输入(每日)而 Listbox2 是当月的输入(每月)的情况下,如何插入日期过滤条件。

这是来自 Excel Sheet1 的原始数据:

ID      Name    Status      Date
1201    Lisa    Pending A   10/14/2024
1202    Lisa    In progress 10/15/2024
1203    Dan     Pending A   10/16/2024
1204    Dan     Pending B   10/17/2024
1205    Dan     Pending C   10/17/2024
1206    Dan     Pending B   10/18/2024
1207    Lisa    Pending B   10/19/2024
1208    Dan     Pending B   10/19/2024
1209    Lisa    Pending A   10/19/2024

在此处输入图片描述

这是得出的代码:

Private Sub UserForm_Initialize()
        
    ' Define constants.
    Const CRITERIA_COLUMN As Long = 3
    
    ' Return the values of the range in an array.
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
    Dim rng As Range:
    Set rng = ws.Range("A1:D" & ws.Cells(ws.Rows.count, "C").End(xlUp).Row)
    Dim sRowsCount As Long: sRowsCount = rng.Rows.count
    Dim ColumnsCount As Long: ColumnsCount = rng.Columns.count
    Dim sData() As Variant: sData = rng.Value
    
    ' Return the matching source row numbers in a collection.
    Dim coll As Collection: Set coll = New Collection
    Dim sr As Long
    For sr = 2 To sRowsCount
        Select Case CStr(sData(sr, CRITERIA_COLUMN))
            Case "Pending A", "Pending B" '**** would like to put a date condition here or anywhere in the whole code to get result
                coll.Add sr
        End Select
    Next sr
    
    ' Define the destination array
    Dim dRowsCount As Long: dRowsCount = coll.count
    If dRowsCount = 0 Then Exit Sub ' no matches
    Dim dData() As Variant: ReDim dData(1 To dRowsCount, 1 To ColumnsCount)
    
    ' Loop through the items (matching source rows) of the collection
    ' to populate the destination array.
    Dim srItem As Variant, dr As Long, c As Long
    For Each srItem In coll
        dr = dr + 1
        For c = 1 To ColumnsCount
            dData(dr, c) = sData(srItem, c)
        Next c
    Next srItem
         
    ' Populate the listbox...
    With Me.ListBox1
        .ColumnHeads = True
        .ColumnWidths = "30,30,50,30"
        .ColumnCount = ColumnsCount
        .List = dData
    End With
    
    With Me.ListBox2
        .ColumnHeads = True
        .ColumnWidths = "30,30,50,30"
        .ColumnCount = ColumnsCount
        '.List = dData
    End With
    
    ' ... and the label.
    'LabelDanDaily.Caption =
    'LabelLisaDaily.Caption =
    
    'LabelDanMonthly.Caption =
    'LabelLisaMonthly.Caption =
    
    'LabelTotalDaily.Caption =
    LabelTotalMonthly.Caption = dRowsCount
        
End Sub

这是所需的输出:

每日每月

如何根据每日和每月日期过滤器获取列表框以及标签中的计数?

excel
  • 1 1 个回答
  • 82 Views

1 个回答

  • Voted
  1. Best Answer
    taller
    2024-10-20T12:53:39+08:002024-10-20T12:53:39+08:00

    添加第二个集合来存储 ListBox2 所需的数据行。

    注意:列表框列标题不能通过.List属性设置。请参考您之前的帖子:

    如何用 VBA 显示列表框中的最后 10 个条目

    Option Explicit
    
    Private Sub UserForm_Initialize()
        
        ' Define constants.
        Const CRITERIA_COLUMN As Long = 3
        Const DATE_COLUMN As Long = 4
        
        ' Return the values of the range in an array.
        Dim wb As Workbook: Set wb = ThisWorkbook
        Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
        Dim rng As Range:
        Set rng = ws.Range("A1:D" & ws.Cells(ws.Rows.Count, "C").End(xlUp).Row)
        Dim sRowsCount As Long: sRowsCount = rng.Rows.Count
        Dim ColumnsCount As Long: ColumnsCount = rng.Columns.Count
        Dim sData As Variant: sData = rng.Value
        
        ' Return the matching source row numbers in a collection.
        Dim coll1 As Collection: Set coll1 = New Collection
        Dim coll2 As Collection: Set coll2 = New Collection
        Dim sr As Long, iDate As Date
        For sr = 2 To sRowsCount
            Select Case CStr(sData(sr, CRITERIA_COLUMN))
            Case "Pending A", "Pending B" '**** would like to put a date condition here or anywhere in the whole code to get result
                If IsDate(sData(sr, DATE_COLUMN)) Then
                    iDate = CDate(sData(sr, DATE_COLUMN))
                    If Month(iDate) = Month(Date) And Year(iDate) = Year(Date) Then
                        coll2.Add sr ' for ListBox2
                        If CDbl(iDate) = CDbl(Date) Then
                            coll1.Add sr ' for ListBox1
                        End If
                    End If
                End If
            End Select
        Next sr
        
        ' Define the destination array
        Dim dData() As Variant
        ' ** for ListBox 2
        If coll2.Count = 0 Then Exit Sub
        Col2Arr coll2, dData, sData, ColumnsCount
        With Me.ListBox2
            .ColumnHeads = False
            .ColumnWidths = "30,30,50,30"
            .ColumnCount = ColumnsCount
            .List = dData
        End With
        ' ** for ListBox 1
        If coll1.Count = 0 Then Exit Sub
        Col2Arr coll1, dData, sData, ColumnsCount
        With Me.ListBox1
            .ColumnHeads = False
            .ColumnWidths = "30,30,50,30"
            .ColumnCount = ColumnsCount
            .List = dData
        End With
        
    End Sub
    
    Sub Col2Arr(ByRef Col As Collection, ByRef dData As Variant, _
            ByVal sData As Variant, ByVal ColumnsCount As Long)
        Dim dRowsCount As Long: dRowsCount = Col.Count
        ReDim dData(1 To dRowsCount + 1, 1 To ColumnsCount)
        Dim c As Long, srItem
        For c = 1 To ColumnsCount
            dData(1, c) = sData(1, c)
        Next
        Dim dr As Long: dr = 1
        For Each srItem In Col
            dr = dr + 1
            For c = 1 To ColumnsCount
                dData(dr, c) = sData(srItem, c)
            Next c
        Next srItem
    End Sub
    

    在此处输入图片描述

    • 1

相关问题

  • 如何返回列出的合同上有费率但系统中没有费率的特定行?

  • 当某些值重复时自动在表中添加参考字段?

  • 循环遍历具有更改单元格地址的列

  • 搜索字符串并输出与该字符串对应的值

  • Excel中有没有一种方法可以计算字符串中特定文本的出现次数,但也包括前一个字符?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve