+ Reply to Thread
Results 1 to 10 of 10

SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    5

    SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Hi,
    I have a master file with multiple rows.
    I'm using VBA to split the master file into individual files each holding data from a single row (iRow).
    When there are no more values in column A (NB: this does not mean that cells are blank as they contain formulas which do not return a TRUE value) the code should exit.
    The individuals files are named after their respective cell (row = iRow, column = A)
    I'm using the macro below however: it creates all individual files (i.e from irow to lastrow) but instead of stopping when it reaches lastrow it gives me an error message saying SaveAs method of Workbook class failed - so in essence it tries to create another file even though it has reached the end of data in that column.

    What am i doing wrong? (I'm not very experienced in VBA so apologies if my mistake is blatantly obvious!)

    Many Thanks
    Roi

    Sub splitfile()

    Dim curWks As Worksheet
    Dim newWks As Worksheet

    Dim iRow As Long
    Dim FirstRow As Long
    Dim LastRow As Long

    Set curWks = Worksheets("Return Format")
    Set newWks = Workbooks.Add(1).Worksheets(1)

    With curWks
    FirstRow = 2 'headers in row 1
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    .Rows(1).Copy
    With newWks.Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With

    For iRow = FirstRow To LastRow
    .Rows(iRow).Copy

    With newWks.Range("A2")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With

    newWks.Parent.SaveAs _
    Filename:=ThisWorkbook.Path & "\" & .Cells(iRow, "A").Value & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Next iRow

    End With

    newWks.Parent.Close savechanges:=False

    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Hi Roi,

    The index always increments - so it will be 1 more than you want it to be after the loop:

    Sub splitfile() 'Roi
    
    Dim curWks As Worksheet
    Dim newWks As Worksheet
    
    Dim iRow As Long
    Dim FirstRow As Long
    Dim LastRow As Long
    
    Set curWks = Worksheets("Return Format")
    Set newWks = Workbooks.Add(1).Worksheets(1)
    
    With curWks
    FirstRow = 2 'headers in row 1
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).row
    
    .Rows(1).Copy
    With newWks.Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With
    
    For iRow = FirstRow To LastRow
    .Rows(iRow).Copy
    
    With newWks.Range("A2")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With
    
    newWks.Parent.SaveAs _
    fileName:=ThisWorkbook.Path & "\" & .Cells(iRow, "A").Value & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Next iRow: iRow = iRow - 1
    
    End With
    
    newWks.Parent.Close savechanges:=False
    
    End Sub
    BTW you need to put code tags around your code: Edit - Highlight your code and hit the #
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    06-04-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Many Thanks!!!

  4. #4
    Registered User
    Join Date
    06-04-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    I have just made the amendment but still gives me the same error message ...

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Can you post a sample? (Go to advanced and click on the paper clip.)

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Maybe:

    Sub splitfile() 'Roi
     
    Dim curWks As Worksheet
    Dim newWks As Worksheet
    
    Dim iRow As Long
    Dim FirstRow As Long
    Dim LastRow As Long
    
    Set curWks = Worksheets("Return Format")
    Set newWks = Workbooks.Add(1).Worksheets(1)
    
    With curWks
    FirstRow = 2 'headers in row 1
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).row
    
    .Rows(1).Copy
    With newWks.Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With
    
    For iRow = FirstRow To LastRow
    If Range("A" & iRow).HasFormula Then Exit For
    .Rows(iRow).Copy
    
    With newWks.Range("A2")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With
    
    newWks.Parent.SaveAs _
    fileName:=ThisWorkbook.Path & "\" & .Cells(iRow, "A").Value & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Next iRow:iRow=iRow-1
    
    End With
    
    newWks.Parent.Close savechanges:=False
    
    End Sub
    Last edited by xladept; 08-12-2013 at 04:39 PM.

  7. #7
    Registered User
    Join Date
    06-04-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Example attached
    Attached Files Attached Files

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    Hi Roi,

    Try this:

    Sub splitfile(): Dim S As String
    
    Dim curWks As Worksheet
    Dim newWks As Worksheet
    
    Dim iRow As Long
    Dim FirstRow As Long
    Dim LastRow As Long
    
    Set curWks = Worksheets("Return Format")
    
    With curWks
    FirstRow = 2 'headers in row 1
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Do Until curWks.Range("A" & LastRow) <> "": LastRow = LastRow - 1: Loop
    Set newWks = Workbooks.Add(1).Worksheets(1)
    
    .Rows(1).Copy
    With newWks.Range("A1")
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
    End With
    
    For iRow = FirstRow To LastRow
    
    .Rows(iRow).Copy
    
        With newWks.Range("A2")
            .PasteSpecial Paste:=xlPasteValues
            .PasteSpecial Paste:=xlPasteFormats
        End With
    S = ThisWorkbook.Path & "\" & .Cells(iRow, "A").Value & ".xlsm"
    newWks.Parent.SaveAs Filename:=S, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Next iRow
    
    End With
    
    newWks.Parent.Close savechanges:=False
    
    End Sub
    It was running over the actual data.
    Last edited by xladept; 08-13-2013 at 03:29 PM.

  9. #9
    Registered User
    Join Date
    06-04-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    This is perfect! Thanks a million!

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: SPLIT .xls IN MULTIPLE FILES BY ROWS AND NAME PER VALUE IN RESPECTIVE CELL

    You're welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Can I split up an excel spreadsheet into multiple files by rows?
    By seespot in forum Excel General
    Replies: 14
    Last Post: 08-28-2017, 04:36 PM
  2. Split Excel file into multiple files based on number of rows
    By kingtut86 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2013, 03:38 PM
  3. [SOLVED] Split Workbook into Multiple Files based on Worksheet Name + Cell Value
    By CRW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-11-2012, 07:05 AM
  4. Split cell into multiple rows
    By lando99 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-20-2012, 04:36 PM
  5. [SOLVED] Ccopy certain cells from other files into respective rows
    By SirRyanGiggs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-14-2012, 01:12 PM

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