update Excel Macro

Hello I would like to add a couple more additions to this macro, but am a little stuck.
1. I would like to update the following macro to copy the values. I am getting #ref! in some of the cells.
2. copy the header over to each new sheet.
3.  Autofit columns A thru G
4. Fit all rows on one page.
 I know some of this can be accomplished with the With ActiveSheet.PageSetup, but I didnt know if that was best practice.


Sub OrigCreate_Separate_Sheet_For_Each_HPageBreak()
    Dim HPB As HPageBreak
    Dim RW As Long
    Dim PageNum As Long
    Dim Asheet As Worksheet
    Dim Nsheet As Worksheet
    Dim Acell As Range

    'Sheet with the data, you can also use Sheets("Sheet1")
    Set Asheet = ActiveSheet

    If Asheet.HPageBreaks.Count = 0 Then
        MsgBox "There are no HPageBreaks"
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'When the macro is ready we return to this cell on the ActiveSheet
    Set Acell = Range("A1")

    'Because of this bug we select a cell below your data
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;210663
    Application.Goto Asheet.Range("A" & Rows.Count), True

    RW = 1
    PageNum = 1

    For Each HPB In Asheet.HPageBreaks
        'Add a sheet for the page
        With Asheet.Parent
            Set Nsheet = Worksheets.Add(after:=.Sheets(.Sheets.Count))
        End With

        'Give the sheet a name
        On Error Resume Next
        Nsheet.name = "Page " & PageNum
        If Err.Number > 0 Then
            MsgBox "Change the name of : " & Nsheet.name & " manually"
            Err.Clear
        End If
        On Error GoTo 0

        'Copy the cells from the page into the new sheet
        With Asheet
            .Range(.Cells(RW, "A"), .Cells(HPB.location.Row - 1, "K")).Copy _
                    Nsheet.Cells(1)
        End With
        ' If you want to make values of your formulas use this line also
         Nsheet.UsedRange.Value = Nsheet.UsedRange.Value

        RW = HPB.location.Row
        PageNum = PageNum + 1
    Next HPB

    Asheet.DisplayPageBreaks = False
    Application.Goto Acell, True

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Open in new window

wiredemc12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
The macro in the attached workbook does the job with the exception of #4 and I need more explanation for that part.
I commented out the Application.Goto Asheet.Range("A" & Rows.Count), True line because it didn't seem to do anything useful and the article that the comment referred to had a bad URL. I also commented out the part of the code that contained the MsgBox "Change the name of : " & Nsheet.Name & " manually" and instead deleted any "Page n" sheets beforehand.
28712522.xlsm
0
wiredemc12Author Commented:
2. Copy the header over to the new sheet. - I didn't see any code in there, but I could be missing something.
4. Here is a small macro that I made that works. Is there a way to incorporate it?

Sub PrintColumns2()
Dim sh As Worksheet
'Dim Awb As Workbook

    Application.ScreenUpdating = False
        ' Prevents screen refreshing.
        
For Each sh In ActiveWorkbook.Worksheets
    sh.Activate

    Application.PrintCommunication = False
    
    With ActiveSheet.PageSetup
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Orientation = xlLandscape
    End With
    Application.PrintCommunication = True
    
    Next sh
End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
I added lines 64 to 66 and 82.
Sub OrigCreate_Separate_Sheet_For_Each_HPageBreak()
    Dim RW As Long
    Dim PageNum As Long
    Dim Asheet As Worksheet
    Dim Nsheet As Worksheet
    Dim Acell As Range
    Dim HPB As HPageBreak
    'Sheet with the data, you can also use Sheets("Sheet1")
    Set Asheet = ActiveSheet

    If Asheet.HPageBreaks.Count = 0 Then
        MsgBox "There are no HPageBreaks"
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'When the macro is ready we return to this cell on the ActiveSheet
    Set Acell = Range("A1")

    'Because of this bug we select a cell below your data
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;210663
    'Application.Goto Asheet.Range("A" & Rows.Count), True

    ' Delete any existing Pages
    Application.DisplayAlerts = False
    For Each Nsheet In Worksheets
        If Left$(Nsheet.Name, 5) = "Page " Then
            Nsheet.Delete
        End If
    Next
    Application.DisplayAlerts = True
    
    RW = 1
    PageNum = 1

    For Each HPB In Asheet.HPageBreaks
        'Add a sheet for the page
        With Asheet.Parent
            Set Nsheet = Worksheets.Add(after:=.Sheets(.Sheets.Count))
        End With

        'Give the sheet a name
'        On Error Resume Next
'        Nsheet.Name = "Page " & PageNum
'        If Err.Number > 0 Then
'            MsgBox "Change the name of : " & Nsheet.Name & " manually"
'            Err.Clear
'        End If
'        On Error GoTo 0
        Nsheet.Name = "Page " & PageNum

        'Copy the cells from the page into the new sheet
        With Asheet
            .Range(.Cells(RW, "A"), .Cells(HPB.Location.Row - 1, "K")).Copy _
                    Nsheet.Cells(1)
        End With
        ' If you want to make values of your formulas use this line also
         Nsheet.UsedRange.Value = Nsheet.UsedRange.Value
        
        Asheet.Rows("1:1").Copy
        Nsheet.Rows("1:1").Insert Shift:=xlDown
        Application.CutCopyMode = False

        Nsheet.Columns("A:G").AutoFit

        RW = HPB.Location.Row
        PageNum = PageNum + 1
    Next HPB

    Asheet.DisplayPageBreaks = False
    Application.Goto Acell, True

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
    PrintColumns2
End Sub

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

wiredemc12Author Commented:
That worked to copy the headers from the top cells, but what I cant figure out is how to get the headers that are in the insert header and footer. Would it be best just to create another macro to insert the header information for each worksheet?
0
Martin LissOlder than dirtCommented:
Added lines 68 to 75.
Sub OrigCreate_Separate_Sheet_For_Each_HPageBreak()
    Dim RW As Long
    Dim PageNum As Long
    Dim Asheet As Worksheet
    Dim Nsheet As Worksheet
    Dim Acell As Range
    Dim HPB As HPageBreak
    'Sheet with the data, you can also use Sheets("Sheet1")
    Set Asheet = ActiveSheet

    If Asheet.HPageBreaks.Count = 0 Then
        MsgBox "There are no HPageBreaks"
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'When the macro is ready we return to this cell on the ActiveSheet
    Set Acell = Range("A1")

    'Because of this bug we select a cell below your data
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;210663
    'Application.Goto Asheet.Range("A" & Rows.Count), True

    ' Delete any existing Pages
    Application.DisplayAlerts = False
    For Each Nsheet In Worksheets
        If Left$(Nsheet.Name, 5) = "Page " Then
            Nsheet.Delete
        End If
    Next
    Application.DisplayAlerts = True
    
    RW = 1
    PageNum = 1

    For Each HPB In Asheet.HPageBreaks
        'Add a sheet for the page
        With Asheet.Parent
            Set Nsheet = Worksheets.Add(after:=.Sheets(.Sheets.Count))
        End With

        'Give the sheet a name
'        On Error Resume Next
'        Nsheet.Name = "Page " & PageNum
'        If Err.Number > 0 Then
'            MsgBox "Change the name of : " & Nsheet.Name & " manually"
'            Err.Clear
'        End If
'        On Error GoTo 0
        Nsheet.Name = "Page " & PageNum

        'Copy the cells from the page into the new sheet
        With Asheet
            .Range(.Cells(RW, "A"), .Cells(HPB.Location.Row - 1, "K")).Copy _
                    Nsheet.Cells(1)
        End With
        ' If you want to make values of your formulas use this line also
         Nsheet.UsedRange.Value = Nsheet.UsedRange.Value
        
        Asheet.Rows("1:1").Copy
        Nsheet.Rows("1:1").Insert Shift:=xlDown
        Application.CutCopyMode = False
        
        With Nsheet.PageSetup
            .LeftHeader = Asheet.PageSetup.LeftHeader
            .CenterHeader = Asheet.PageSetup.CenterHeader
            .RightHeader = Asheet.PageSetup.RightHeader
            .LeftFooter = Asheet.PageSetup.LeftFooter
            .CenterFooter = Asheet.PageSetup.CenterFooter
            .RightFooter = Asheet.PageSetup.RightFooter
        End With
        
        Nsheet.Columns("A:G").AutoFit

        RW = HPB.Location.Row
        PageNum = PageNum + 1
    Next HPB

    Asheet.DisplayPageBreaks = False
    Application.Goto Acell, True

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
    PrintColumns2
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.