Solved

split excel file into multiple files based on page breaks

Posted on 2013-01-24
5
469 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 48

Expert Comment

by:Rgonzo1971
Comment Utility
Hi,

How do you determine the names of the new files?

Regards
0
 

Author Comment

by:AndreasHermle
Comment Utility
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 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

763 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

7 Experts available now in Live!

Get 1:1 Help Now