Solved

# How to split data in excel into equal groups? (excel vba)

Posted on 2016-10-28
135 Views
Hi everyone,

I have a big amount of data in excel (many rows, many columns) and I need to devide it into equal groups. The main problem is that the amount of groups is something that I cannot predict, meaning that on day I need to split it into 12 groups other day into 15. Additionally, the amount of columns and rows also may change, depends on the kind of report we receive. What I need is macro(?) which will allow me to to devide data into separate tabs, but the amount of the tabs is something I need to choose. All data provided in the report need to move.

Is anyone able to help me?

Report.xlsx
0
Question by:Marta Wilczyńska
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 47

Expert Comment

ID: 41864083
Can you give an example of, or tell us how you want it divided up?
0

LVL 92

Accepted Solution

Patrick Matthews earned 500 total points (awarded by participants)
ID: 41864391
This appears to do it.  Assumptions:
• Prompted for number tabs to split to at start
• If # of data rows is evenly divisible by # tabs, each tab will have same # rows
• If not, The first (n-1) tabs will have the same # rows, and the "extra" rows go to the nth tab

``````Option Explicit

Sub SplitToTabs()

Dim LastR As Long, LastC As Long
Dim SourceWs As Worksheet
Dim DestWb As Workbook
Dim DestWs As Worksheet
Dim NumOfTabs As Long
Dim NumDataRows As Long
Dim RowsPerTab As Long
Dim OldSheetsPerWb As Long
Dim Counter As Long
Dim RowsToCopy As Long
Dim StartCopyRow As Long

Set SourceWs = ActiveSheet
With SourceWs
LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
NumDataRows = LastR - 1

NumOfTabs = InputBox("You have " & NumDataRows & " data rows", "How many tabs to split into?", 1)
RowsPerTab = Int(NumDataRows / NumOfTabs)

OldSheetsPerWb = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = NumOfTabs
Application.SheetsInNewWorkbook = OldSheetsPerWb
StartCopyRow = 2

For Counter = 1 To NumOfTabs
Set DestWs = DestWb.Worksheets(Counter)
SourceWs.Cells(1, 1).Resize(1, LastC).Copy DestWs.Cells(1, 1)
If Counter < NumOfTabs Then
RowsToCopy = RowsPerTab
Else
RowsToCopy = LastR - StartCopyRow + 1
End If
SourceWs.Cells(StartCopyRow, 1).Resize(RowsToCopy, LastC).Copy DestWs.Cells(2, 1)
StartCopyRow = StartCopyRow + RowsToCopy
Next

End Sub
``````
1

Author Comment

ID: 41871700
Patrick it works! Thank you very much
0

LVL 92

Expert Comment

ID: 41897212
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
###### Suggested Courses
Course of the Month4 days, 5 hours left to enroll