筛选出所有的牛肉:
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