Consulting

Results 1 to 4 of 4

Thread: Layout list by VBA

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Layout list by VBA

    Hi eveyone i need your help regarding This excel sheet


    On Sheet1 i have our stok list and page layout is A5 and want each page have only the same shelfs codes, (if it is going more than on epage will do the same) shelf codes are in column D.
    when run VBA code it must create 2 row in top of each page and in fist row put shelf code in column A and also copy the first row in sheet2 to sheet1 in second empty row.
    you can see sample picture.



    Note : if we can add border for cells and column for each page will be great.




    Thanks for your try and effort .
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Try this


    Option Explicit
    
    
    Sub Reformat()
        Dim aryColD() As String
        Dim cntColD As Long, i As Long, rowLast As Long
        
        Application.ScreenUpdating = False
        
        With Worksheets("Sheet1")
        
            .ResetAllPageBreaks
            
            rowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
        
            cntColD = 1
            ReDim Preserve aryColD(1 To cntColD)
            aryColD(cntColD) = 1
            
            For i = 2 To rowLast - 1
                If .Cells(i, 4).Value <> .Cells(i + 1, 4).Value Then
                    cntColD = cntColD + 1
                    ReDim Preserve aryColD(1 To cntColD)
                    aryColD(cntColD) = i + 1
                End If
            Next i
    
    
            For i = UBound(aryColD) To LBound(aryColD) Step -1
                Application.StatusBar = "Processing row " & i
                Worksheets("Sheet2").Rows(1).Copy
                .Rows(aryColD(i)).Insert Shift:=xlDown
                
                .Rows(aryColD(i)).Insert Shift:=xlDown
                .Cells(aryColD(i), 1).Value = .Cells(aryColD(i) + 2, 4).Value
                .Cells(aryColD(i), 1).Font.Bold = True
                If aryColD(i) > 1 Then .HPageBreaks.Add Before:=.Rows(aryColD(i))
            Next i
        End With
    
    
        Application.StatusBar = False
        Application.ScreenUpdating = True
        
        MsgBox "Done"
    
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location
    @Paul_Hossler You saved me ! really appriciate for your great help . Good Job and thanks for your effort and time ! .

    Just possible add border to cell and column excep the first row

    Last edited by parscon; 12-21-2021 at 01:03 AM.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Option Explicit
    
    
    Sub Reformat()
        Dim aryColD() As String
        Dim cntColD As Long, i As Long, rowLast As Long, rowLastInBlock As Long
        
        Application.ScreenUpdating = False
        
        With Worksheets("Sheet1")
        
            .ResetAllPageBreaks
            
            rowLast = .Cells(.Rows.Count, 1).End(xlUp).Row
        
            cntColD = 1
            ReDim Preserve aryColD(1 To cntColD)
            aryColD(cntColD) = 1
            
            For i = 2 To rowLast - 1
                If .Cells(i, 4).Value <> .Cells(i + 1, 4).Value Then
                    cntColD = cntColD + 1
                    ReDim Preserve aryColD(1 To cntColD)
                    aryColD(cntColD) = i + 1
                End If
            Next i
    
    
            For i = UBound(aryColD) To LBound(aryColD) Step -1
                Application.StatusBar = "Processing row " & i
                Worksheets("Sheet2").Rows(1).Copy
                .Rows(aryColD(i)).Insert Shift:=xlDown
                
                .Rows(aryColD(i)).Insert Shift:=xlDown
                .Cells(aryColD(i), 1).Value = .Cells(aryColD(i) + 2, 4).Value
                .Cells(aryColD(i), 1).Font.Bold = True
                
                rowLastInBlock = .Cells(aryColD(i), 1).End(xlDown).Row
                
                With .Cells(aryColD(i) + 1, 1).Resize(rowLastInBlock - aryColD(i), 7).Borders
                    .LineStyle = xlContinuous
                    .ColorIndex = 0
                    .TintAndShade = 0
                    .Weight = xlThin
                End With
                
                If aryColD(i) > 1 Then .HPageBreaks.Add Before:=.Rows(aryColD(i))
            Next i
        End With
    
    
        Application.StatusBar = False
        Application.ScreenUpdating = True
        
        MsgBox "Done"
    
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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