如何用VBA筛选同一个人某一天出现2次一般诊疗费的数据并提取当天的所有数据
时间: 2024-06-03 18:10:32 浏览: 10
您可以使用VBA编写以下代码来筛选同一个人某一天出现2次一般诊疗费的数据并提取当天的所有数据:
Sub extractData()
Dim lastRow As Long
Dim i As Long, j As Long
Dim nameCol As Long, dateCol As Long, feeCol As Long
Dim name As String, dateStr As String
Dim feeCount As Integer
'设置表格列号
nameCol = 1
dateCol = 2
feeCol = 3
lastRow = ActiveSheet.Cells(Rows.Count, nameCol).End(xlUp).Row '获取最后一行
'循环检查每一行的数据
For i = 2 To lastRow
name = Cells(i, nameCol).Value
dateStr = Format(Cells(i, dateCol).Value, "yyyy-mm-dd")
feeCount = 0
'内层循环统计同一个人同一天的一般诊疗费的数量
For j = 2 To lastRow
If Cells(j, nameCol).Value = name And Format(Cells(j, dateCol).Value, "yyyy-mm-dd") = dateStr And Cells(j, feeCol).Value = "一般诊疗费" Then
feeCount = feeCount + 1
End If
Next j
'如果数量为2,则提取当天所有数据
If feeCount = 2 Then
For j = 1 To 3 '假设数据只有3列
Cells(i, j).Copy Cells(i + 1, j) '将当前行复制到下一行
Next j
End If
Next i
End Sub
请注意,此代码的前提是您已经将数据整理成表格,并且数据包含姓名、日期和费用三列。在实际使用时,请根据实际情况调整代码。另外,建议在使用前先备份数据,以免出现意外情况。