Solved

split excel file into multiple files based on page breaks

Posted on 2013-01-24
5
482 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 51

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 51

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

739 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