两个方案,
一个是公式,采用原始的字符串拼接——
- =A2&":"&IF(B2="未打卡",B$1,"")&IF(C2="未打卡","、"&C$1,"")&IF(D2="未打卡","、"&D$1,"")&IF(E2="未打卡","、"&E$1,"")&IF(F2="未打卡","、"&F$1,"")&IF(G2="未打卡","、"&G$1,"")&IF(H2="未打卡","、"&H$1,"")&IF(I2="未打卡","、"&I$1,"")&IF(J2="未打卡","、"&J$1,"")&IF(K2="未打卡","、"&K$1,"")&IF(L2="未打卡","、"&L$1,"")&IF(M2="未打卡","、"&M$1,"")&IF(N2="未打卡","、"&N$1,"")&IF(O2="未打卡","、"&O$1,"")&IF(P2="未打卡","、"&P$1,"")&IF(Q2="未打卡","、"&Q$1,"")&IF(R2="未打卡","、"&R$1,"")&IF(S2="未打卡","、"&S$1,"")&IF(T2="未打卡","、"&T$1,"")&IF(U2="未打卡","、"&U$1,"")&IF(V2="未打卡","、"&V$1,"")&IF(COUNTIF(B2:V2,"未打卡"),"日未打卡","正常出勤")
复制代码
另一个方案,采用自定义函数——
代码如下:
- Function wdk(r1 As Range, r2 As Range, r3 As Range)
- On Error Resume Next
- Dim a%, i%, j%, arr
- If Not r1 Is Nothing And Not r2 Is Nothing And Not r3 Is Nothing And r2.Cells.Count = r3.Cells.Count Then
- a = Application.CountIf(r2, "未打卡") '统计“未打卡”次数
- ReDim arr(a - IIf(a > 0, 1, 0)) '定义未打卡数据存放数组的维度
- For i = 1 To r2.Cells.Count '循环检测考勤时间区域,记录“未打卡”数据对应的日期,并写入数组arr()
- If r2.Cells(i) = "未打卡" Then
- arr(j) = r3.Cells(i)
- j = j + 1
- End If
- Next
- wdk = r1 & ":" & IIf(j > 0, Join(arr, "、") & "日未打卡", "正常出勤") '合并字符串
- Else
- wdk = "错误:单元格引用参数不正确!"
- End If
- End Function
复制代码 |