|
楼主 |
发表于 2023-3-7 18:18
|
显示全部楼层
Sub SplitStudentsByClass()
Dim currentRow As Integer
currentRow = 2
Dim currentClass As String
currentClass = Mid(Range("A" & currentRow).Value, 5, 2)
' 创建第一个工作表
Dim currentSheet As Worksheet
Set currentSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
currentSheet.Name = "班级 " & currentClass
' 复制表头
Range("A1:B1").Copy currentSheet.Range("A1:B1")
' 复制学生信息
Dim i As Integer
For i = 2 To Range("A1").End(xlDown).Row
If Mid(Range("A" & i).Value, 5, 2) <> currentClass Then
' 切换到下一个班级
currentClass = Mid(Range("A" & i).Value, 5, 2)
Set currentSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
currentSheet.Name = "班级 " & currentClass
Range("A1:B1").Copy currentSheet.Range("A1:B1")
currentRow = 2
End If
Range("A" & i & ":B" & i).Copy currentSheet.Range("A" & currentRow & ":B" & currentRow)
currentRow = currentRow + 1
Next i
End Sub
|
|