Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

split excel file into multiple files based on page breaks

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
AndreasHermle
Asked:
AndreasHermle
  • 3
  • 2
1 Solution
 
Rgonzo1971Commented:
Hi,

How do you determine the names of the new files?

Regards
0
 
AndreasHermleAuthor Commented:
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
 
Rgonzo1971Commented:
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
 
AndreasHermleAuthor Commented:
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
 
AndreasHermleAuthor Commented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now