将excel文件分成较小的文件(每行5000行),而无需将行解组

时间:2019-05-07 07:15:15

标签: excel vba divide

我需要将具有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. 第58条

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

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

0 个答案:

没有答案