Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

split excel file into multiple files based on page breaks

Posted on 2013-01-24
5
Medium Priority
?
486 Views
Last Modified: 2013-01-27
Dear Experts:

I got a huge file where I have more than 300 manually inserted page breaks.

Is it possible by means of VBA code to split this file into multiple/individual files (more than 300) based on these page breaks.

Help is more than appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
[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
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 38813753
Hi,

How do you determine the names of the new files?

Regards
0
 

Author Comment

by:AndreasHermle
ID: 38813814
Hi rgonzo,

thank you very much for your quick feedback.

for example the cell value/contents in Column B  directly below the page breaks.

Thank you

Regards, Andreas
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 38813943
Hi,

Maybe you could use this:
 (only the manual breaks are considered)

Sub SplitByPageBreaks()

Dim strFolder As String
Dim aRowList() As Long
Dim aColList() As Long
ReDim aRowList(10000)
ReDim aColList(10000)

Dim idxRowList As Integer
Dim idxColList As Integer

aRowList(0) = 1
aColList(0) = 1
idxRowList = 1
idxColList = 1

Application.ScreenUpdating = False


Set myRange = ActiveSheet.UsedRange

With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        
        If .SelectedItems.Count > 0 Then
            strFolder = .SelectedItems(1)
        Else
            MsgBox " No folder choosed"
            Exit Sub
        End If
End With

For Each rw In myRange.Rows
    If rw.PageBreak = xlPageBreakManual Then
       aRowList(idxRowList) = rw.Row
       idxRowList = idxRowList + 1
    End If
Next rw

For Each cl In myRange.Columns
    If cl.PageBreak = xlPageBreakManual Then
       aColList(idxColList) = cl.Column
       idxColList = idxColList + 1
    End If
Next cl

aRowList(idxRowList) = myRange.Rows.Count + 1
aColList(idxColList) = myRange.Columns.Count + 1

ReDim Preserve aRowList(idxRowList)
ReDim Preserve aColList(idxColList)


For idxRow = 0 To UBound(aRowList) - 1
    For idxCol = 0 To UBound(aColList) - 1
        
            ActiveSheet.Copy Before:=Sheets(1)
            ActiveSheet.Name = "Temp"
        
        Set newRange = Range(Cells(aRowList(idxRow), aColList(idxCol)), _
            Cells(aRowList(idxRow + 1) - 1, aColList(idxCol + 1) - 1))
            addr = newRange.Address
            c = newRange.Column
        Set thisrange = Range(Range("A" & newRange.Row + newRange.Rows.Count), _
            Range("A" & newRange.Row + newRange.Rows.Count - 1).End(xlDown))
            thisrange.EntireRow.Delete
        Set thisrange = Range(Cells(1, newRange.Column + newRange.Columns.Count), Cells(1, newRange.Column + newRange.Columns.Count - 1).End(xlToRight))
            thisrange.EntireColumn.Delete
        
        If newRange.Row <> 1 Then
            Set thisrange = Range(Range("A1"), Range("A" & newRange.Row - 1))
            thisrange.EntireRow.Delete
        End If
        
        If newRange.Column <> 1 Then
            Set thisrange = Range(Range("A1"), Cells(1, newRange.Column - 1))
            thisrange.EntireColumn.Delete

        End If
        
        ActiveSheet.Copy
        ActiveWorkbook.SaveAs Filename:= _
        strFolder & "\" & Range("B1").Text & ".xlsm", FileFormat:= _
        xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

        ActiveWindow.Close
                 
        Application.DisplayAlerts = False
        Sheets("Temp").Delete
        Application.DisplayAlerts = True
        
        Sheets("Sheet1").Select
            
    Next ' idxCol
Next ' idxRow

Application.ScreenUpdating = True
End Sub

Open in new window


Regards
0
 

Author Comment

by:AndreasHermle
ID: 38814130
Hi rgonzo,

thank you very much for your great support. Initial testing showed that it worked partly well.

I will do some more thorough testing and then let you knwo.

again, thank you very much for your professional help. I really appreciate it.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 38817833
Hi rgonzo,

thank you very much for your great and professional support. I tweaked the code a tiny bit and added 'On Error Resume Next' and it worked just fine.

Great job rgonzo, thank you very much.

Regards, Andreas
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

705 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