Consulting

Results 1 to 7 of 7

Thread: Multiple condition page break

  1. #1
    VBAX Newbie
    Joined
    Mar 2025
    Posts
    2
    Location

    Multiple condition page break

    I have a Worksheet that I import a CSV file into and then use vba to format. the first column has a date and time format which can consist of multiple days (ie. 3/11/2025 13:21 and 3/12/2025 17:56) I need to have each day have its own page and each page can only have 25 rows. a page can have less rows but no more than 25. I can write up a code to page break at 25 but not sure how i can page break by date and 25 rows per page. any help would be greatly appreciated.
    Attached Images Attached Images

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,379
    Location
    Does this work for you?

    Sub InsertPageBreaksByDateAndRowCount()
        Dim ws As Worksheet
        Dim lastRow As Long
        Dim i As Long
        Dim rowCount As Long
        Dim currentDate As Date
        ' Set the worksheet you are working with
        Set ws = ThisWorkbook.Sheets("YourSheetName") ' Change "YourSheetName" to the actual sheet name
        ' Find the last row in Column A with data
        lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
        ' Initialize variables  rowCount = 0
        currentDate = ws.Cells(1, "A").Value 
        ' Assume the first date is in A1
        ' Loop through the rows
        For i = 1 To lastRow
            ' Check for date change
            If ws.Cells(i, "A").Value <> currentDate Then
                ' Insert a page break before the current row if it's not the first row
                If i > 1 Then
                    ws.Rows(i).PageBreak = xlPageBreakManual
                End If
                currentDate = ws.Cells(i, "A").Value
                rowCount = 1 
                ' Reset row count for the new date
            Else
                rowCount = rowCount + 1
            End If
            ' Check if the row count for the current page exceeds 25
            If rowCount > 25 Then
                ' Insert a page break before the current row
                ws.Rows(i).PageBreak = xlPageBreakManual
                rowCount = 1 
                ' Reset row count for the new page
            End If
        Next i
        MsgBox "Page breaks inserted based on date changes and a maximum of 25 rows per page.", vbInformation
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,835
    Location
    In your screen shot the Amount column appears to have a Daily Total.

    Is that in the CSV?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Newbie
    Joined
    Mar 2025
    Posts
    2
    Location
    This worked Perfect, Thank you Very much

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,379
    Location
    Thank you for asking in this forum.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    You mentioned you can break at 25 rows, but do you reset the count when a new date starts?

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,379
    Location
    Welcome to VBAX henaleona. If your post was directed to me then look at line 24 in the module.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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