|
宏代码如下所示——
- Option Explicit
- Sub SyncData()
- Application.ScreenUpdating = False '关闭屏幕刷新
- Dim rng As Range, arr, i%, j%, wst As Worksheet, w As Worksheet
- With ActiveSheet
- For Each rng In .Range("E2:E" & .[E2].End(xlDown).Row)
- arr = Split(Replace(rng.Value, ",", ","), ",") '提取培训人员到数组,并做中西文逗号的排错兼容处理
- For i = 0 To UBound(arr)
- j = 0
- For Each w In ThisWorkbook.Sheets '检测是否存在子表
- If w.Name = arr(i) Then j = j + 1
- Next
- If j > 0 Then '存在子表则在子表内添加数据
- Set wst = Sheets(arr(i))
- Else '不存在子表则新建子表,并以姓名命名
- Set wst = ThisWorkbook.Sheets.Add(after:=Sheets(ThisWorkbook.Sheets.Count))
- wst.Name = arr(i)
- End If
- If Application.CountIf(.Range(.[E2], rng), "*" & arr(i) & "*") = 1 Then '初始化分表
- wst.Cells.ClearContents
- wst.[A1:D1] = Array("课程代码", "课程名称", "培训单位", "培训时间")
- End If
- rng.Offset(0, -4).Resize(1, 4).Copy wst.[A65536].End(xlUp).Offset(1, 0) '以复制的方式分发数据
- Next i
- Next
- .Activate
- Application.CutCopyMode = False '关闭复制模式
- Set wst = Nothing
- End With
- Application.ScreenUpdating = True '打开屏幕刷新
- End Sub
复制代码 |
|