+ Reply to Thread
Results 1 to 8 of 8

page break or page set up in macro

Hybrid View

  1. #1
    Registered User
    Join Date
    10-15-2011
    Location
    bangalore
    MS-Off Ver
    Excel 2010
    Posts
    99

    page break or page set up in macro

    Hi friends,

    in this code i dividing one single sheet data into different sheets , based on the producer name into a sheet. now i have attached the file i need the page break to be given after every reporting period in the sheet like wise in all the sheets.


    Sub sep_producer()
    Dim prodname As String
    Dim templrow As Long
    Dim j As Long
    
    StRow = 1
    lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lrow + 1
        If Worksheets("Sheet1").Range("A" & i).Value Like "Report Period*" And i <> 1 Or i = lrow + 1 Then
            prodname = Right(Worksheets("Sheet1").Range("A" & StRow + 4).Value, Len(Worksheets("Sheet1").Range("A" & StRow + 4).Value) - 10)
            If Not Evaluate("ISREF('" & prodname & "'!A1)") Then
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = prodname
            End If
            templrow = Worksheets(prodname).Range("A" & Rows.Count).End(xlUp).Row
            Worksheets("Sheet1").Range("A" & StRow & ":H" & RowCount).Copy
            Worksheets(prodname).Range("A" & templrow + 2).PasteSpecial Paste:=xlValues
            Worksheets(prodname).Range("A" & templrow + 2).PasteSpecial Paste:=xlFormats
            Worksheets(prodname).Cells.EntireColumn.AutoFit
            Worksheets(prodname).Columns("H:H").ColumnWidth = 37.14
            Worksheets(prodname).Cells.EntireRow.AutoFit
            StRow = i
            RowCount = RowCount + 1
        ElseIf Worksheets("Sheet1").Range("A" & i).Value Like "Report Period*" And i = 1 Then
            RowCount = RowCount + 1
        Else
            RowCount = RowCount + 1
        End If
     
    Next i
    
    For j = 1 To Worksheets.Count
        With Worksheets(j)
            If .Range("A1") = "" And .Range("A2") = "" Then
                .Rows("1:2").Delete
            End If
        End With
    Next j
    
    End Sub

  2. #2
    Registered User
    Join Date
    10-15-2011
    Location
    bangalore
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: page break or page set up in macro

    can some body help me on this

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: page break or page set up in macro

    Hey Somusas,

    Sorry for the delay.

    I have included the page break and printarea code in the original code itself -
    Option Explicit
    Dim Scount As Integer
    Dim StRow As Long
    Dim i As Long
    Dim lrow As Long
    Dim RowCount As Long
    Dim prodname As String
    Dim templrow As Long
    Dim j As Long
    
    Sub sep_producer()
    
    StRow = 1
    lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 1 To lrow + 1
        If Worksheets("Sheet1").Range("A" & i).Value Like "Report Period*" And i <> 1 Or i = lrow + 1 Then
            prodname = Right(Worksheets("Sheet1").Range("A" & StRow + 4).Value, Len(Worksheets("Sheet1").Range("A" & StRow + 4).Value) - 10)
            If Not Evaluate("ISREF('" & prodname & "'!A1)") Then
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = prodname
            End If
            templrow = Worksheets(prodname).Range("A" & Rows.Count).End(xlUp).Row
            Worksheets("Sheet1").Range("A" & StRow & ":H" & RowCount).Copy
            If Worksheets(prodname).Range("A1") = "" Then
                Worksheets(prodname).Range("A1").PasteSpecial Paste:=xlValues
                Worksheets(prodname).Range("A1").PasteSpecial Paste:=xlFormats
            Else
                Worksheets(prodname).Range("A" & templrow + 2).PasteSpecial Paste:=xlValues
                Worksheets(prodname).Range("A" & templrow + 2).PasteSpecial Paste:=xlFormats
            End If
            Worksheets(prodname).Cells.EntireColumn.AutoFit
            Worksheets(prodname).Columns("H:H").ColumnWidth = 37.14
            Worksheets(prodname).Cells.EntireRow.AutoFit
            StRow = i
            RowCount = RowCount + 1
            Worksheets(prodname).HPageBreaks.Add before:=Range("A" & i)
        ElseIf Worksheets("Sheet1").Range("A" & i).Value Like "Report Period*" And i = 1 Then
            RowCount = RowCount + 1
        Else
            RowCount = RowCount + 1
        End If
        
    Next i
    
    For j = 1 To Worksheets.Count
        With Worksheets(j)
            .PageSetup.PrintArea = ""
        End With
    Next j
    
    End Sub
    Last edited by arlu1201; 01-10-2012 at 10:50 AM.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Registered User
    Join Date
    10-15-2011
    Location
    bangalore
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: page break or page set up in macro

    its wroking fine in horizantal , now vertical became two pages , firts page break come in D it should be in H

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: page break or page set up in macro

    Try adding this line -
    Worksheets(prodname).VPageBreaks.Add after:=Range("G" & i)
    after this line
    Worksheets(prodname).HPageBreaks.Add before:=Range("A" & i)

  6. #6
    Registered User
    Join Date
    10-15-2011
    Location
    bangalore
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: page break or page set up in macro

    thanks arlu , but this not working , is there any other way

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: page break or page set up in macro

    What errors are you getting?

  8. #8
    Registered User
    Join Date
    10-15-2011
    Location
    bangalore
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: page break or page set up in macro

    there are two page breaks coming one at D and other at H

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1