excel VBA示例-间隔复制多行到新的sheet中

需求: sheet1中包含N列。将数据分为m份,每一份存入新的sheet中,具体为:
第一个sheet:  1,1+m,2+m,3+m ..
第二个sheet: 2,2+m,3+m...
...
直接上VBA代码:

Sub AddWorksheetAfterLast(ByVal name As String)
On Error Resume Next
Worksheets.Add(After:=Worksheets(Worksheets.count)).name = name
End Sub


Sub Process()
Dim count As Integer
Dim sheetName As String
Dim index As Integer

'count = InputBox("请输入分割的份数!")
count = 5

all_Columns = Sheets("root").UsedRange.Rows.count

Debug.Print "all_Columns:" & all_Columns

For i = 1 To count
sheetName = "sheet_" & i


AddWorksheetAfterLast (sheetName)
'开始处理
index = 1
For j = i To all_Columns Step count
Debug.Print j
Sheets("root").UsedRange.Rows(j).Copy Sheets(sheetName).UsedRange.Rows(index)
index = index + 1
'Sheets("root").UsedRange.Rows(j).Select

Next j
Next i
End Sub

附件:sample
| 0个评论