+ Reply to Thread
Results 1 to 8 of 8

Keep printing until all values in a column is printed out - Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    04-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2003/2007
    Posts
    45

    Keep printing until all values in a column is printed out - Macro

    Hi. I have a scenario that I would like to accomplish but need help from people who know a lot of Macro or VBA.


    Sheet1:
    A1:A300 (not consistent)
    1 - I would like to have a macro that will copy 10 cells at a time from Sheet1 to Sheet2 (A1:B10)
    2 - Then once transferred, Sheet2 is automatically printed
    3 - Then once printed, copy the other 10 cells (this time it would be A11:A20 to Sheet2 (A1:B10)
    4 - repeat step 1 to 3 until everything is printed out. Now as mentioned above, A1:A300 is not consistent, it could be A1:A20 or A1:A799, etc.

    Summary: I would like to print all info in Sheet1 using the format in Sheet2 - that is 10 values at a time. Thank you.

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Keep printing until all values in a column is printed out - Macro

    Try this:
    Change Sheet2 with the the name of the second sheet.

    Sub Print10s()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim i As Long, r As Long
    Set sh1 = ThisWorkbook.ActiveSheet
    Set sh2 = ThisWorkbook.Worksheets("Sheet2")
    r = sh1.Range("A" & sh1.Rows.Count).End(xlUp).Row
    i = 1
    sh1.PageSetup.PrintArea = "A1:A10"
    With sh2
        Do While i <= r
            .Range("A1:A10").Value = sh1.Range("A" & i & ":A" & i + 9).Value
            sh2.PrintOut
            i = i + 10
        Loop
    End With
    
    End Sub
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Keep printing until all values in a column is printed out - Macro

    Hello itsmejan24,

    Welcome to the Forum!

    The macro below will automatically find the end of column "A" on "Sheet1". The contents of these cells will be copied into cell "A1:B10" on "Sheet2", printed out and then repeated until there is no more. Data

    You can change the worksheet names from "Sheet1" and "Sheet2" to what you are actually using. Not sure if the cell addresses on "Sheet2" were a typo but "A1:B10" is twenty cells, not ten.

    Add a new VBA module to your workbook then copy and paste the code below into it.
    
    Sub CopyAndPrint()
    
        Dim DstRng As Range
        Dim DstWks As Worksheet
        Dim i As Long
        Dim n As Long
        Dim R As Long
        Dim Rng As Range
        Dim RngEnd As Range
        Dim SrcRng As Range
        Dim SrcWks As Worksheet
        
            Set SrcWks = Worksheets("Sheet1")
            Set DstWks = Worksheets("Sheet2")
            
            Set SrcRng = SrcWks.Range("A1")
            Set DstRng = DstWks.Range("A1:B10")
            
                Set RngEnd = SrcWks.Cells(Rows.Count, SrcRng.Column).End(xlUp)
                If RngEnd.Row < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcWks.Range(SrcRng, RngEnd)
                
                For R = 1 To SrcRng.Rows.Count Step 10
                    Set Rng = SrcRng.Rows(R).Resize(10, 1)
                        For n = 1 To Rng.Rows.Count Step 2
                            i = i + 1
                            DstRng.Cells(i, 1) = Rng.Cells(n, 1)
                            DstRng.Cells(i, 2) = Rng.Cells(n + 1, 1)
                        Next n
                    i = 0
                    DstWks.PrintOut
                    DstRng.ClearContents
                Next R
                
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    04-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2003/2007
    Posts
    45

    Re: Keep printing until all values in a column is printed out - Macro

    Hi. Sheet2 now is printing okay - A1:B10 info are all printed perfectly. I would like to ask another favor. Is it possible to put an empty cell in between the row. Let say, when it copies the info on A1 and B1, A2 and B2 should be empty and the next info should be copied into A3 and B3 and so on....

    Thank you again in advance.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Keep printing until all values in a column is printed out - Macro

    Hello itsmejan24,

    Here is the update to add a blank between the lines on "Sheet2". The added change is in bold.
    
    Sub CopyAndPrint()
    
        Dim DstRng As Range
        Dim DstWks As Worksheet
        Dim i As Long
        Dim n As Long
        Dim R As Long
        Dim Rng As Range
        Dim RngEnd As Range
        Dim SrcRng As Range
        Dim SrcWks As Worksheet
        
            Set SrcWks = Worksheets("Sheet1")
            Set DstWks = Worksheets("Sheet2")
            
            Set SrcRng = SrcWks.Range("A1")
            Set DstRng = DstWks.Range("A1:B10")
            
                Set RngEnd = SrcWks.Cells(Rows.Count, SrcRng.Column).End(xlUp)
                If RngEnd.Row < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcWks.Range(SrcRng, RngEnd)
                
                For R = 1 To SrcRng.Rows.Count Step 10
                    Set Rng = SrcRng.Rows(R).Resize(10, 1)
                        For n = 1 To Rng.Rows.Count Step 2
                            i = i + 1
                            DstRng.Cells(i, 1) = Rng.Cells(n, 1)
                            DstRng.Cells(i, 2) = Rng.Cells(n + 1, 1)
                            i = i + 1
                        Next n
                    i = 0
                    DstWks.PrintOut
                    DstRng.ClearContents
                Next R
                
    End Sub

  6. #6
    Registered User
    Join Date
    04-05-2012
    Location
    Canada
    MS-Off Ver
    Excel 2003/2007
    Posts
    45

    Re: Keep printing until all values in a column is printed out - Macro

    Leith Ross and Kelshaer .....It worked perfectly!!! Thank you very much for your help. I really appreciated it.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Keep printing until all values in a column is printed out - Macro

    Please mark the thread as solved

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Keep printing until all values in a column is printed out - Macro

    Try this
    Sub Print10s()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim i As Long, r As Long
    Set sh1 = ThisWorkbook.ActiveSheet
    Set sh2 = ThisWorkbook.Worksheets("Sheet2")
    r = sh1.Range("A" & sh1.Rows.Count).End(xlUp).Row
    Dim c As Byte
    sh1.PageSetup.PrintArea = "A1:B20"
    With sh2
        .Range("A1:B20").Value = ""
        Do While i < r
            For c = 1 To 20 Step 2
                i = i + 1
                .Range("A" & c & ":B" & c).Value = sh1.Range("A" & i).Value
            Next
            sh2.PrintOut
        Loop
    End With
    End Sub
    Last edited by Kelshaer; 04-10-2012 at 01:16 PM.

+ 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