Excel:vba实现筛选数据

筛选出所有的牛肉:

Sub 筛选出牛肉()
Dim lastRow As Long
Dim n%
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

For n = lastRow To 2 Step -1
    If Cells(n, "b").Value = "牛肉" Then
        '"牛肉"的单元格向左偏移一个单元格,则将改行的第1列到第3列一起粘贴到e列开始的一个空行中
        Cells(n, "b").Offset(0, -1).Resize(1, 3).Copy Cells(Rows.Count, "e").End(xlUp).Offset(1, 0)
        '也可以用Range(Cells(n, 1), Cells(n, 3)).Copy Cells(Rows.Count, "e").End(xlUp).Offset(1, 0)
        '意思是,将该行的第1列到第3列一起粘贴到e列开始的一个空行中
    End If
Next n
MsgBox "数据处理完毕"
End Sub

 筛选出近几天的牛肉:(我这里是筛选出三天的数据)

Sub 筛选出近三天牛肉()
Dim lastRow As Long
Dim n%, day%
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

day = 0
For n = lastRow To 2 Step -1
    If Cells(n, "b").Value = "牛肉" Then
            Cells(n, "b").Offset(0, -1).Resize(1, 3).Copy Cells(Rows.Count, "e").End(xlUp).Offset(1, 0)
            day = day + 1
        If day = 3 Then Exit For '超过三天直接退出
    End If
Next n
MsgBox "数据处理完毕"
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值