首页 > 解决方案 > 将 excel 文件分成 5000 行的较小文件,每个文件不拆分行

问题描述

我需要将 60000+ 行的 excel 表分成不同的文件,每个文件大约 5000 行。插入的数据是分组的,我需要新表不要拆分这些组,但我无法做到这一点,也无法每次都创建一个新文件。

我尝试使用以下代码,但无法使其正常运行:

Public Sub split()
Dim sheetnumber, t As Double
line = 0
k = lastrow 'this gives the last full row in the file

sheetnumber = WorksheetFunction.RoundUp(k / 5000, 1)

For i = 1 To sheetnumber Step 1

line = line + 5000

article = CStr(Sheets("sheet1").Cells(linea, 1).Value)
For m = 0 To 30 Step 1
    If article = CStr(Sheets("sheet1").Cells(linea + m, 1).Value) Then
    GoTo e
    Else
    line = line + m
    Exit For
    End If
e:

Next m
Sheets.Add After:=ActiveSheet
For n = 1 To line Step 1
   ActiveSheet.Row(n) = Sheets("sheet1").Row(n) ' i also tried to put a cycle here to 
'copy the whole row and then go to the next one but the rest won't work
Next n

ActiveSheet.Select
ActiveSheet.Move


Next i

End Sub

该文件的组成如下:

  1. 第 0 条
  2. 第1条
  3. 第1条
  4. 第1条
  5. 第 2 条
  6. 第 2 条
  7. 第 3 条

...

  1. 第 56 条
  2. 第 57 条
  3. 第 57 条
  4. 第 57 条
  5. 第 57 条
  6. 第五十八条

我需要新文件包含从 5002 到 1 的所有行,另一个文件包含从 10003 到 5002 的所有行,依此类推到文件末尾(最后一行约为 60000),而不会破坏文章组。

感谢您提供任何建议/帮助

标签: excelvbadivide

解决方案


推荐阅读