阅: 901 | 回: 2
发表于2023/8/7 11:50:26
楼主
等级:初学者
- 积分:6
- 财富值:49.3
- 身份:普通用户
以Sheet2工作表第一组(第2、第3、第4行)数据为例,逻辑步骤如下:
说明:Sheet1工作表A列数据为不重复的编号数据。
1、根据Sheet2工作表A列A2单元格里的条件,筛选出Sheet1工作表B列符合条件的数据;(B列数据总共有7类:"BJCK"、“GZCK”、“HKCK”、“NJCK”、“SHCK”、“SZCK”、“WHCK”);
2、根据Sheet2工作表B列B2单元格里的条件,筛选出Sheet1工作表C列符合条件的数据;(C列数据总共有7类:"A3"、“B2、“D1”、“S1”、“V1”、“Y1”、“Y2”);
3、根据Sheet2工作表C列C2单元格里的条件,筛选出Sheet1工作表D列符合条件(此处举例“自营电商”)的数据,接着继续筛选出Sheet1工作表E列“已出库”的数据;(D列数据总共有3类:"自营电商"、“合作代理商”、“线下旗舰店”);
4、第2行数据:根据Sheet2工作表D1单元格里的条件,筛选出Sheet1工作表F列符合条件的数据,再以Sheet1工作表上述筛选的A列数据进行计数,将计数数量写入Sheet2工作表D1到O1日期下方对应的第2行单元格。
5、第3行数据:取消Sheet1工作表E列的筛选,以Sheet1工作表上述筛选的A列数据进行计数,将计数数量写入Sheet2工作表D1到O1日期下方对应的第3行单元格。
6、第4行数据:使用(总数-出库数)/总数,得出出库率,将出库率写入Sheet2工作表D1到O1日期下方对应的第4行单元格。
我的个性签名
等级:学有小成
- 积分:23
- 财富值:2
- 身份:普通用户
以下是一个示例的VBA代码,用于实现你描述的逻辑步骤:
```vba
Sub FilterAndCountData()
Dim ws1 As Worksheet ' Sheet1
Dim ws2 As Worksheet ' Sheet2
Dim rng1 As Range ' Sheet1数据范围
Dim rng2 As Range ' Sheet2数据范围
Dim filterValue1 As String ' Sheet2 A列条件值
Dim filterValue2 As String ' Sheet2 B列条件值
Dim filterValue3 As String ' Sheet2 C列条件值
Dim filterValue4 As String ' Sheet2 D1单元格条件值
Dim countValue As Long ' 计数值
Dim totalCount As Long ' 总数
Dim outCount As Long ' 出库数
Dim outRate As Double ' 出库率
Dim i As Long ' 循环变量
' 设置工作表对象
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' 获取Sheet1数据范围
Set rng1 = ws1.Range("A1").CurrentRegion
' 获取Sheet2数据范围
Set rng2 = ws2.Range("A2:C4")
' 遍历Sheet2第一组数据
For i = 2 To 4
' 获取条件值
filterValue1 = rng2.Cells(i, 1).Value
filterValue2 = rng2.Cells(i, 2).Value
filterValue3 = rng2.Cells(i, 3).Value
filterValue4 = rng2.Cells(i, 4).Value
' 筛选Sheet1数据
rng1.AutoFilter Field:=2, Criteria1:=filterValue1
rng1.AutoFilter Field:=3, Criteria1:=filterValue2
rng1.AutoFilter Field:=4, Criteria1:=filterValue3
rng1.AutoFilter Field:=5, Criteria1:=filterValue4 & "*"
' 计算总数
totalCount = rng1.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
' 计算出库数
outCount = rng1.Columns(5).SpecialCells(xlCellTypeVisible).Count - 1
' 计算出库率
If totalCount > 0 Then
outRate = (totalCount - outCount) / totalCount
Else
outRate = 0
End If
' 将计数值和出库率写入Sheet2
ws2.Cells(2, i + 3).Value = totalCount
ws2.Cells(3, i + 3).Value = outCount
ws2.Cells(4, i + 3).Value = outRate
Next i
' 清除筛选
rng1.AutoFilter
' 清除对象引用
Set rng1 = Nothing
Set rng2 = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
End Sub
```
请注意,上述代码仅供参考,你可能需要根据实际情况进行调整和补充。在运行代码之前,请确保工作簿中存在名为"Sheet1"和"Sheet2"的工作表,并按照你的描述在Sheet2的A2:C4范围内填入相应的条件值。
```vba
Sub FilterAndCountData()
Dim ws1 As Worksheet ' Sheet1
Dim ws2 As Worksheet ' Sheet2
Dim rng1 As Range ' Sheet1数据范围
Dim rng2 As Range ' Sheet2数据范围
Dim filterValue1 As String ' Sheet2 A列条件值
Dim filterValue2 As String ' Sheet2 B列条件值
Dim filterValue3 As String ' Sheet2 C列条件值
Dim filterValue4 As String ' Sheet2 D1单元格条件值
Dim countValue As Long ' 计数值
Dim totalCount As Long ' 总数
Dim outCount As Long ' 出库数
Dim outRate As Double ' 出库率
Dim i As Long ' 循环变量
' 设置工作表对象
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
' 获取Sheet1数据范围
Set rng1 = ws1.Range("A1").CurrentRegion
' 获取Sheet2数据范围
Set rng2 = ws2.Range("A2:C4")
' 遍历Sheet2第一组数据
For i = 2 To 4
' 获取条件值
filterValue1 = rng2.Cells(i, 1).Value
filterValue2 = rng2.Cells(i, 2).Value
filterValue3 = rng2.Cells(i, 3).Value
filterValue4 = rng2.Cells(i, 4).Value
' 筛选Sheet1数据
rng1.AutoFilter Field:=2, Criteria1:=filterValue1
rng1.AutoFilter Field:=3, Criteria1:=filterValue2
rng1.AutoFilter Field:=4, Criteria1:=filterValue3
rng1.AutoFilter Field:=5, Criteria1:=filterValue4 & "*"
' 计算总数
totalCount = rng1.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
' 计算出库数
outCount = rng1.Columns(5).SpecialCells(xlCellTypeVisible).Count - 1
' 计算出库率
If totalCount > 0 Then
outRate = (totalCount - outCount) / totalCount
Else
outRate = 0
End If
' 将计数值和出库率写入Sheet2
ws2.Cells(2, i + 3).Value = totalCount
ws2.Cells(3, i + 3).Value = outCount
ws2.Cells(4, i + 3).Value = outRate
Next i
' 清除筛选
rng1.AutoFilter
' 清除对象引用
Set rng1 = Nothing
Set rng2 = Nothing
Set ws1 = Nothing
Set ws2 = Nothing
End Sub
```
请注意,上述代码仅供参考,你可能需要根据实际情况进行调整和补充。在运行代码之前,请确保工作簿中存在名为"Sheet1"和"Sheet2"的工作表,并按照你的描述在Sheet2的A2:C4范围内填入相应的条件值。
敬天爱人,成人达已