Solved

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

Posted on 2016-10-28
4
30 Views
Last Modified: 2016-11-22
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?

Thank you in advance.
Report.xlsx
0
Comment
Question by:Marta Wilczyńska
  • 2
4 Comments
 
LVL 45

Expert Comment

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

Accepted Solution

by:
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
    Set DestWb = Workbooks.Add
    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

Open in new window

1
 

Author Comment

by:Marta Wilczyńska
ID: 41871700
Patrick it works! Thank you very much
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 41897212
author acknowledged answer
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is an Add-On procedure to be used in conjunction with the code provided in Reducing EE Email Clutter using Outlook (http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/A_3146-Outlook-Processing-EE-emails-on-Receive.…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now