With Worksheets(sheet_name) Dim row_count, temp, i row_count = 0 For i = start_row + 1 To end_row temp = Range(col_name & i).Value If temp = Range(col_name & (i - 1)).Value Then sheet_map(1, sheet_index) = sheet_map(1, sheet_index) + 1 Else ReDim Preserve sheet_map(1, sheet_index + 1) sheet_index = sheet_index + 1 sheet_map(0, sheet_index) = temp sheet_map(1, sheet_index) = 1 End If Next End With
'根据前面计算的拆分表,拆分成单个文件 Dim row_index row_index = start_row For i = 0 To sheet_index Workbooks.Add '创建最终数据文件夹 Dim dir_name dir_name = ThisWorkbook.Path & "\拆分出的表格\" If Dir(dir_name, vbDirectory) = "" Then MkDir (dir_name) End If '创建新工作簿 Dim workbook_path workbook_path = ThisWorkbook.Path & "\拆分出的表格\" & sheet_map(0, i) & ".xls" ActiveWorkbook.SaveAs Filename:=workbook_path, FileFormat:=-4143 ActiveSheet.Name = sheet_map(0, i) '激活当前工作簿,ThisWorkbook表示当前跑代码的工作簿 ThisWorkbook.Activate