10 outputs on single sheet

amkkhan

Board Regular
Joined
Dec 11, 2021
Messages
75
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Here is the link to file
Situation here is similar to this one
Actually "ID" is the main Identity.
I am using Vlookup for calling students data and picture.
Actually these are students cards. For printing purpose,
All I want is10 cards to be printed on single page.

As you can see that output is very small size.
Page size must be A4 or legal.

I need a VB Script that would scroll ID's from Consolidated Sheets Column A and almost 1000 entries. If list becomes blank at any point it should stop.
And output must be a pdf file containing All provided students in the "Consolidated" sheet with 10 outputs on each page.

I have data of 34 students for example but it could be around 1000 or more ...
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hey @John_w ,
Last time your script was awesome.
I will always remain thankful to you.
Situation here is similar to your last script. Here I need 10 outputs on A4 or legal sheet for printing purpose. Please
 
Upvote 0
Hi... @HaHoBe
Can you please help me on it.
I am looking forward with a good expectation
 
Upvote 0
The "Cards" sheet contains a single student 'card'.

The card for each of the 34 students would be generated according to the student ID and copied to a temporary sheet for output as a single PDF, with 10 cards per page. But what is the layout of the 10 cards on the temporary sheet that would give you 10 cards per page? It seems a card is too large to fit 10 cards on a page. Please post the 10 cards layout in this thread using the XL2BB Add-in tool.
 
Upvote 0
The "Cards" sheet contains a single student 'card'.

The card for each of the 34 students would be generated according to the student ID and copied to a temporary sheet for output as a single PDF, with 10 cards per page. But what is the layout of the 10 cards on the temporary sheet that would give you 10 cards per page? It seems a card is too large to fit 10 cards on a page. Please post the 10 cards layout in this thread using the XL2BB Add-in tool.
Hi @John_w , in the image below I tired to make the output very simple for you. As you can see from the excel file that "Card" sheet's output is going to be very small and it is never going to exceed the Standard Card Size.
 

Attachments

  • IMG_20240115_204417.jpg
    IMG_20240115_204417.jpg
    148.9 KB · Views: 12
Upvote 0
The "Cards" sheet contains a single student 'card'.

The card for each of the 34 students would be generated according to the student ID and copied to a temporary sheet for output as a single PDF, with 10 cards per page. But what is the layout of the 10 cards on the temporary sheet that would give you 10 cards per page? It seems a card is too large to fit 10 cards on a page. Please post the 10 cards layout in this thread using the XL2BB Add-in tool.
Output Page Orientation should be Horizontal please
 
Upvote 0
The layout screenshot with the 10 blue rectangles doesn't really help. It needs to show 10 actual cards.

My understanding is that a single card is cells A2:B9 on the Cards sheet. However, I can only fit 6 cards on a page - 2 cards per 'row' and 3 rows of cards:

Loop rows update template sheet create single PDF with 10 per page .xlsm
ABCD
1ID4004ID4628
2
3NameStudent 1NameStudent 2
4F.NameF.Name 1F.NameF.Name 2
5Class/Sec1Class/Sec1
6Phone00097987987Phone00876876876
7AddressDowntownAddressCity
8PhotoPhoto
9ID4476ID4806
10
11NameStudent 3NameStudent 4
12F.NameF.Name 3F.NameF.Name 4
13Class/Sec1Class/Sec1
14Phone00876876786Phone05434243534
15AddressDowntownAddressCity
16PhotoPhoto
17ID4507ID4082
18
19NameStudent 5NameStudent 6
20F.NameF.Name 5F.NameF.Name 6
21Class/Sec1Class/Sec1
22Phone53342545645Phone45678657646
23AddressDowntownAddressCity
24PhotoPhoto
25ID4380ID4984
26
27NameStudent 7NameStudent 8
28F.NameF.Name 7F.NameF.Name 8
29Class/Sec1Class/Sec1
30Phone00352424778Phone43543247543
31AddressDowntownAddressCity
32PhotoPhoto
33ID4697ID4869
34
35NameStudent 9NameStudent 10
36F.NameF.Name 9F.NameF.Name 10
37Class/Sec1Class/Sec2
38Phone59878574440Phone76876876786
39AddressDowntownAddressCity
40PhotoPhoto
Sheet2
Cells with Data Validation
CellAllowCriteria
B1List=Consolidated!$A$2:$A$35
D1List=Consolidated!$A$2:$A$35
B9List=Consolidated!$A$2:$A$35
D9List=Consolidated!$A$2:$A$35
B17List=Consolidated!$A$2:$A$35
D17List=Consolidated!$A$2:$A$35
B25List=Consolidated!$A$2:$A$35
D25List=Consolidated!$A$2:$A$35
B33List=Consolidated!$A$2:$A$35
D33List=Consolidated!$A$2:$A$35


In my sheet above there is an automatic page break at the bottom of row 31, above the photos for student 6 and 7.

Please post your sheet using XL2BB with 10 cards per page so I can see the exact layout.

Output Page Orientation should be Horizontal please
Sorry, I don't understand what you mean.
 
Upvote 0
Thanks @John_w
Let me explain everything to you briefly step by step
1) This is the old result cards Macro Enabled Excel file. That was generating results(all results combined in a single pdf and file saved at desired location. You should download and check it once.

2) You written a VB Script for me during making of above-mentioned file. Script you made is also still available here.

3) This is another Excel file and this time I am learning to make student cards. As you can see that output is very small.

4) I need your help to write me a similar VB Script that would save 10 outputs on a single page and will make output for 1000 entries. As last script was making result of al students. Sample output for cards 1 page is here

5) All I need is a similar VB Script that would generate all the cards for students provided in "Consolidated" sheet and print 10 outputs on single page and makes all the cards in the same way.
I hope you understand now
 
Upvote 0
I think I understand what you're asking for, however your last post didn't really clarify anything or add further explanation.

It looks like each 'card' should be rotated by 90 degrees and then 10 cards (each card is cells A2:B9 on the "Cards" sheet) placed in a 2 columns x 5 rows 'grid' will fit onto one A4 page. If that's correct then try the macro below.

This proved quite tricky because I discovered that if you position a shape (a picture of a 'card') at (0,0) on a sheet - the first card needs to be at (0,0) - and rotate it 90 degrees, the width and height of the shape changes! The solution is to first position the shape well away from (0,0) so that rotating it doesn't change its width and height and then move it to the correct position. Furthermore, when the rotated shape is explicitly positioned at (0,0) its resultant Top property value is 0, but its Left is actually (Width - Height) / 2, not 0, and visibly the left side of the shape is on the exact left of the sheet, but the top of the shape is not at the top of the sheet, as it should be!

1705581691190.png


The shape is positioned and then adjusted by the following code:
VBA Code:
        'Set shape's correct position
       
        .Top = picPositionTop
        .Left = picPositionLeft
        .IncrementTop (picWidth - picHeight) / 2 'move up
        If picPositionLeft > 0 Then .IncrementLeft (picHeight - picWidth) / 2 'move right

1705581738674.png


VBA Code:
Public Sub Create_Cards_PDF()
   
    Dim PDFoutputFile As String
    Dim PDFsheet As Worksheet
    Dim currentSheet As Worksheet
    Dim r As Long, n As Long
    Dim cardRange As Range
    Dim picPositionTop As Single, picPositionLeft As Single
    Dim picShape As Shape
    Dim pageBreakCell As Range
    Dim gridlines As Boolean
   
    '2 cards per row x 5 rows per page gives 10 cards per page
    Const CardsPerRow = 2
    Const RowsPerPage = 5

    'Gap between rows and columns of cards on output PDF
    Const CardRowGap = 4
    Const CardColumnGap = 4
 
    PDFoutputFile = ThisWorkbook.Path & "\All cards with 10 cards per page.pdf"
       
    Application.ScreenUpdating = False
   
    'The single card is A2:B9 on the Cards sheet
   
    With Worksheets("Cards")
        Set cardRange = Worksheets("Cards").Range("A2:B9")
        .Select
        gridlines = ActiveWindow.DisplayGridlines
        ActiveWindow.DisplayGridlines = False  'turn off gridlines, otherwise they appear in the PDF
    End With

    With ActiveWorkbook
        Set currentSheet = .ActiveSheet
        Set PDFsheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        ActiveWindow.DisplayGridlines = False
    End With
   
    picPositionTop = 0
    picPositionLeft = 0
   
    With Worksheets("Consolidated")

        n = 0
        r = 2
       
        While Not IsEmpty(.Cells(r, "A").Value)
                 
            'Create a picture of the next card
           
            Worksheets("Cards").Range("B2").Value = .Cells(r, "A").Value
            cardRange.Copy
            With cardRange.Worksheet
                .Pictures.Paste
                Set picShape = .Shapes(.Shapes.Count)
            End With
           
            'Add the picture to the PDF sheet in the correct position
           
            Add_Card_Picture picShape, PDFsheet, picPositionTop, picPositionLeft
           
            n = n + 1
           
            If n Mod CardsPerRow = 0 Then
                'Set top position for next card on a new row
                picPositionTop = picPositionTop + picShape.Width + CardRowGap
                picPositionLeft = 0
            Else
                'Set left position for next card on the same row
                picPositionLeft = picPositionLeft + picShape.Height + CardColumnGap
            End If
           
            If n Mod CardsPerRow * RowsPerPage = 0 Then
                'All cards on a page so insert page break
                Set pageBreakCell = GetCellByPos(PDFsheet, 0, picPositionTop)
                'Debug.Print "Page break " & pageBreakCell.EntireRow.Address
                PDFsheet.HPageBreaks.Add Before:=pageBreakCell.EntireRow
                picPositionTop = pageBreakCell.Top
            End If
           
            r = r + 1
        Wend
       
    End With
   
    Application.PrintCommunication = False
    With PDFsheet.PageSetup
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
    End With
    Application.PrintCommunication = True
   
    'Save PDF sheet as a .pdf file
   
    PDFsheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFoutputFile, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
   
    Application.DisplayAlerts = False
    PDFsheet.Delete
    Application.DisplayAlerts = True
   
    Worksheets("Cards").Select
    ActiveWindow.DisplayGridlines = gridlines  'restore gridlines setting
   
    currentSheet.Select
   
    Application.ScreenUpdating = True
   
    MsgBox "Created " & PDFoutputFile
 
End Sub


Private Sub Add_Card_Picture(picShape As Shape, destSheet As Worksheet, picPositionTop As Single, picPositionLeft As Single)

    Dim picWidth As Single, picHeight As Single
   
    'Cut source card shape and paste on PDF sheet
   
    picShape.Cut
    With destSheet
        .Select
        .Paste
        Set picShape = .Shapes(.Shapes.Count)
    End With
   
    With picShape

        picWidth = .Width
        picHeight = .Height
       
        'Set shape's position away from (0,0) so that rotating it 90 degrees does not change its width and height
       
        .Top = .Width
        .Left = .Height
       
        'Debug.Print "Before rotate top=" & .Top & ", left=" & .Left & ", width=" & .Width & ", height=" & .Height
       
        'Rotate shape 90 degrees to put card sideways
       
        .LockAspectRatio = msoTrue
        .Rotation = 90
       
        'Debug.Print "After rotate top=" & .Top & ", left=" & .Left & ", width=" & .Width & ", height=" & .Height
       
        'Set shape's correct position
       
        .Top = picPositionTop
        .Left = picPositionLeft
        .IncrementTop (picWidth - picHeight) / 2 'move up
        If picPositionLeft > 0 Then .IncrementLeft (picHeight - picWidth) / 2 'move right
       
        'Debug.Print "After reposition top=" & .Top & ", left=" & .Left & ", width=" & .Width & ", height=" & .Height
       
    End With

End Sub


Private Function GetCellByPos(ws As Worksheet, x As Single, y As Single) As Range
    With ws.Shapes.AddLine(x, y, x, y)
        Set GetCellByPos = .TopLeftCell
        .Delete
    End With
End Function
 
Upvote 1
Solution
I think I understand what you're asking for, however your last post didn't really clarify anything or add further explanation.

It looks like each 'card' should be rotated by 90 degrees and then 10 cards (each card is cells A2:B9 on the "Cards" sheet) placed in a 2 columns x 5 rows 'grid' will fit onto one A4 page. If that's correct then try the macro below.

This proved quite tricky because I discovered that if you position a shape (a picture of a 'card') at (0,0) on a sheet - the first card needs to be at (0,0) - and rotate it 90 degrees, the width and height of the shape changes! The solution is to first position the shape well away from (0,0) so that rotating it doesn't change its width and height and then move it to the correct position. Furthermore, when the rotated shape is explicitly positioned at (0,0) its resultant Top property value is 0, but its Left is actually (Width - Height) / 2, not 0, and visibly the left side of the shape is on the exact left of the sheet, but the top of the shape is not at the top of the sheet, as it should be!

View attachment 105306

The shape is positioned and then adjusted by the following code:
VBA Code:
        'Set shape's correct position
      
        .Top = picPositionTop
        .Left = picPositionLeft
        .IncrementTop (picWidth - picHeight) / 2 'move up
        If picPositionLeft > 0 Then .IncrementLeft (picHeight - picWidth) / 2 'move right

View attachment 105307

VBA Code:
Public Sub Create_Cards_PDF()
  
    Dim PDFoutputFile As String
    Dim PDFsheet As Worksheet
    Dim currentSheet As Worksheet
    Dim r As Long, n As Long
    Dim cardRange As Range
    Dim picPositionTop As Single, picPositionLeft As Single
    Dim picShape As Shape
    Dim pageBreakCell As Range
    Dim gridlines As Boolean
  
    '2 cards per row x 5 rows per page gives 10 cards per page
    Const CardsPerRow = 2
    Const RowsPerPage = 5

    'Gap between rows and columns of cards on output PDF
    Const CardRowGap = 4
    Const CardColumnGap = 4
 
    PDFoutputFile = ThisWorkbook.Path & "\All cards with 10 cards per page.pdf"
      
    Application.ScreenUpdating = False
  
    'The single card is A2:B9 on the Cards sheet
  
    With Worksheets("Cards")
        Set cardRange = Worksheets("Cards").Range("A2:B9")
        .Select
        gridlines = ActiveWindow.DisplayGridlines
        ActiveWindow.DisplayGridlines = False  'turn off gridlines, otherwise they appear in the PDF
    End With

    With ActiveWorkbook
        Set currentSheet = .ActiveSheet
        Set PDFsheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
        ActiveWindow.DisplayGridlines = False
    End With
  
    picPositionTop = 0
    picPositionLeft = 0
  
    With Worksheets("Consolidated")

        n = 0
        r = 2
      
        While Not IsEmpty(.Cells(r, "A").Value)
                
            'Create a picture of the next card
          
            Worksheets("Cards").Range("B2").Value = .Cells(r, "A").Value
            cardRange.Copy
            With cardRange.Worksheet
                .Pictures.Paste
                Set picShape = .Shapes(.Shapes.Count)
            End With
          
            'Add the picture to the PDF sheet in the correct position
          
            Add_Card_Picture picShape, PDFsheet, picPositionTop, picPositionLeft
          
            n = n + 1
          
            If n Mod CardsPerRow = 0 Then
                'Set top position for next card on a new row
                picPositionTop = picPositionTop + picShape.Width + CardRowGap
                picPositionLeft = 0
            Else
                'Set left position for next card on the same row
                picPositionLeft = picPositionLeft + picShape.Height + CardColumnGap
            End If
          
            If n Mod CardsPerRow * RowsPerPage = 0 Then
                'All cards on a page so insert page break
                Set pageBreakCell = GetCellByPos(PDFsheet, 0, picPositionTop)
                'Debug.Print "Page break " & pageBreakCell.EntireRow.Address
                PDFsheet.HPageBreaks.Add Before:=pageBreakCell.EntireRow
                picPositionTop = pageBreakCell.Top
            End If
          
            r = r + 1
        Wend
      
    End With
  
    Application.PrintCommunication = False
    With PDFsheet.PageSetup
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
    End With
    Application.PrintCommunication = True
  
    'Save PDF sheet as a .pdf file
  
    PDFsheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFoutputFile, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  
    Application.DisplayAlerts = False
    PDFsheet.Delete
    Application.DisplayAlerts = True
  
    Worksheets("Cards").Select
    ActiveWindow.DisplayGridlines = gridlines  'restore gridlines setting
  
    currentSheet.Select
  
    Application.ScreenUpdating = True
  
    MsgBox "Created " & PDFoutputFile
 
End Sub


Private Sub Add_Card_Picture(picShape As Shape, destSheet As Worksheet, picPositionTop As Single, picPositionLeft As Single)

    Dim picWidth As Single, picHeight As Single
  
    'Cut source card shape and paste on PDF sheet
  
    picShape.Cut
    With destSheet
        .Select
        .Paste
        Set picShape = .Shapes(.Shapes.Count)
    End With
  
    With picShape

        picWidth = .Width
        picHeight = .Height
      
        'Set shape's position away from (0,0) so that rotating it 90 degrees does not change its width and height
      
        .Top = .Width
        .Left = .Height
      
        'Debug.Print "Before rotate top=" & .Top & ", left=" & .Left & ", width=" & .Width & ", height=" & .Height
      
        'Rotate shape 90 degrees to put card sideways
      
        .LockAspectRatio = msoTrue
        .Rotation = 90
      
        'Debug.Print "After rotate top=" & .Top & ", left=" & .Left & ", width=" & .Width & ", height=" & .Height
      
        'Set shape's correct position
      
        .Top = picPositionTop
        .Left = picPositionLeft
        .IncrementTop (picWidth - picHeight) / 2 'move up
        If picPositionLeft > 0 Then .IncrementLeft (picHeight - picWidth) / 2 'move right
      
        'Debug.Print "After reposition top=" & .Top & ", left=" & .Left & ", width=" & .Width & ", height=" & .Height
      
    End With

End Sub


Private Function GetCellByPos(ws As Worksheet, x As Single, y As Single) As Range
    With ws.Shapes.AddLine(x, y, x, y)
        Set GetCellByPos = .TopLeftCell
        .Delete
    End With
End Function
Thanks @John_w 😊,
I salute your skills
I shall be always great ful to you.
It worked exactly as you said. For the sake of information please guide me that isn't it possible that we output Page could be set as Horizontal instead of vertical.
I mean to say that is it possible to change page Orientation from portrait to landscape, then printing would be much easier.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top