+ Reply to Thread
Results 1 to 12 of 12

Split data set by 500 rows help

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    6

    Split data set by 500 rows help

    Hi guys, I am new here and I need some help regarding a splitting rows macro


    Description:
    1st part: To count how many entries are filled (by column A) not counting the 1st row
    2nd part: divide the number above by 500
    3rd part: after divided by 500, it will be rounded up (1501/500 will be 4)
    4th part: create a new excel file per 500 (4 excel file will be created if there's 1501 rows), each excel file will only have 1 tab on the bottom left instead of the original 3 when you 1st create an excel file, and the tab name in each file would be 2-500, and 501-1000 for the 2nd created excel file (1st row being the topic that will display in all the created excel file)
    5th part:generate the data into the newly created files (row 2 to 500 in 1st created file, row 501-1000 in 2nd file) (row 1 of each excel file created should be the row 1 of the original document)
    6th part: save each excel file on to desktop and the name will be row 2-500.xls and so forth


    Do you think this will be a long project or something a noob like me can't handle

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Split data set by 500 rows help

    you can handle it, for each part you can search with Google and find examples.

    you can count the entries with
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    numentries = lastrow -1
    Last edited by patel45; 08-02-2012 at 02:04 PM.

  3. #3
    Registered User
    Join Date
    08-02-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Split data set by 500 rows help

    I am having trouble
    how do i make it so that the division would be rounded up (aka 501/500 = 2)
    I checked on google theres a roundup command, but when i input it into excel 2007, it didn't seem to work

  4. #4
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: Split data set by 500 rows help

    Looks like a short project to me. Here is code you can use as an example to get parts 1-3 done.
    Sub Macro1()
    Dim rowCount As Long, newWBCount As Integer, k As Integer
    Dim wbStartRow As Long, tmp As String
    Dim wbNames() As String
    ReDim wbNames(1)
    
        rowCount = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp) - 1
        
        newWBCount = Application.WorksheetFunction.RoundUp(rowCount / 500, 0)
    
        wbNames(0) = "2-500"
        
        wbStartRow = 501
        
        For k = 1 To newWBCount - 1
            ReDim Preserve wbNames(UBound(wbNames) + 1)
            wbNames(UBound(wbNames)) = CStr(wbStartRow) & "-" & CStr(wbStartRow + 499)
            wbStartRow = wbStartRow + 500
        Next
        
        For k = 0 To UBound(wbNames)
            tmp = tmp & wbNames(k) & vbCrLf
        Next
        
        MsgBox (k - 1) & " names of new workbooks to be added are: " & vbCrLf & tmp
    
    End Sub

  5. #5
    Registered User
    Join Date
    08-02-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Split data set by 500 rows help

    ^Omg thank you, i was trying it out and turned out i was missing the word "Application"

    Now i am stuck at the forth part , i did some research and was told to try the loop function, but i don't really get it

  6. #6
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: Split data set by 500 rows help

    Refer to the For loop near the bottom of my code. If you choose to store the list of new workbook names in an array, as I have done,
    then you could add the new workbooks using a For loop. I've created a list of workbook names inside the loop. You would replace that line
    of code with code to add new workbooks, and the names would be those names stored in the array wbNames. In the loop, the variable k
    allows you to access each element of the array, so if you calculated that you need 7 workbooks, this loop will run 7 times.
    If you have over 1,000 rows, the first loop will give you the name for wbName(0), which will be "2-500", and the next time through
    the loop will give you the name for wbName(1), and that name will be "501-1000", etc. The loop continues until there are no more
    workbook names in the array.

    I used the loop to build a list--you will use the same starting and ending line of this loop and replace the code in the middle
    to add each new workbook.

  7. #7
    Registered User
    Join Date
    08-02-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Split data set by 500 rows help

    I am sorry if I sound noob but wheres the loop near the bottom of your code?

  8. #8
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: Split data set by 500 rows help

    For k = 0 To UBound(wbNames)
            tmp = tmp & wbNames(k) & vbCrLf
        Next

  9. #9
    Registered User
    Join Date
    08-02-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Split data set by 500 rows help

    I am sorry, I am still confuse about the fourth part

  10. #10
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: Split data set by 500 rows help

    Copy the attached Excel 2003 workbook into its own folder and run the macro. Study the code
    and you'll see how I've created the series of new workbooks.

    The code below can replace what is in the attachment. I reworked it to
    eliminate unnecessary lines and steps.

    Sub Create_WBSeries()
    'create new workbook for each 500 data rows in Sheet1 column A
    'name each workbook based on a numbered group, i.e., "2-500.xls", "501-1000.xls", etc.
    'Exception: 1st workbook to be named "2-500.xls"
    'Workbooks not created if they are found in the destination folder.
    '
    Dim rowCount As Long, newWbCount As Integer, k As Integer, wbAddedCount As Integer
    Dim wbStartRow As Long, tmp As String
    Dim isNewWorkbook As Boolean
    Dim NewBook As Workbook
    Dim wbNames() As String
    ReDim wbNames(1)
    
        rowCount = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp) - 1
        
        newWbCount = Application.WorksheetFunction.RoundUp(rowCount / 500, 0)
        wbNames(0) = "2-500"
        
        wbStartRow = 501
        
        For k = 1 To newWbCount - 1
            ReDim Preserve wbNames(UBound(wbNames) + 1)
            wbNames(UBound(wbNames) - 1) = CStr(wbStartRow) & "-" & CStr(wbStartRow + 499)
            wbStartRow = wbStartRow + 500
        Next
        
        Application.ScreenUpdating = False
        For k = 0 To UBound(wbNames) - 1
            
            isNewWorkbook = Dir(ThisWorkbook.Path & "\" & wbNames(k) & ".xls") <> ""
            If Not isNewWorkbook Then
                Set NewBook = Workbooks.Add
                NewBook.SaveAs Filename:=ThisWorkbook.Path & "\" & wbNames(k) & ".xls"
                ActiveWindow.Close
                wbAddedCount = wbAddedCount + 1
                tmp = tmp & wbNames(k) & vbCrLf
            End If
        Next
        Application.ScreenUpdating = True
        
        tmp = wbAddedCount & " new workbooks have been added: " & vbCrLf & tmp & vbCrLf & _
            "in the folder " & ThisWorkbook.Path
        If wbAddedCount = 0 Then tmp = tmp & vbCrLf & vbCrLf & "All workbooks were created previously."
        
        MsgBox tmp
    
        Set NewBook = Nothing
        
    End Sub
    Attached Files Attached Files
    Last edited by xLJer; 08-07-2012 at 01:11 PM. Reason: grammar

  11. #11
    Registered User
    Join Date
    08-02-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Split data set by 500 rows help

    i tried putting the marco in my excel, (2007) but it said debug during the
    "rowCount = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp) - 1"
    is it because the above is a Excel 2003 code?

  12. #12
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: Split data set by 500 rows help

    Change:
    rowCount = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp) - 1
    to this
    rowCount = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Rows - 1
    It works either way in my Excel 2003 file.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1