Solved

split excel file into multiple files based on page breaks

Posted on 2013-01-24
5
471 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
  • 3
  • 2
5 Comments
 
LVL 49

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 49

Accepted Solution

by:
Rgonzo1971 earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

910 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

21 Experts available now in Live!

Get 1:1 Help Now