14

I made a VBA script that will read values from one sheet and create a "label" on another sheet.
This label is supposed to be printed on a special paper that is split in three parts.

Since I live in Sweden we use the A4 paper size (297x210 mm). The lables are supposed to be 99x210 mm.
This means each value needs to be printed on the exact position on the paper.

I do this for my company, thus all coputers are exactly the same.
Same model, same version of Windows, same version of Excel.

This is a smal part of the code (what is relevant to the positioning of text)

For i = 2 To Lastrow

        ' Location name
        Sheets("Etikett").Range("A" & intRad) = Sheets("Bins").Range("A" & i)
        With Sheets("Etikett").Range("A" & intRad & ":K" & intRad)
            .MergeCells = True
            .Font.Color = clr 
            .Font.Size = 150
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .BorderAround Weight:=xlThick
            .Borders.Color = clr
            .Borders(xlEdgeLeft).Weight = xlThick ' this may look odd but is needed
            .Borders(xlEdgeRight).Weight = xlThick
        End With

        'Checknumber
        Sheets("Etikett").Range("B" & intRad + 1) = Sheets("Bins").Range("B" & i)
        With Sheets("Etikett").Range("B" & intRad + 1 & ":D" & intRad + 1)
            .MergeCells = True
            .Font.Color = clr
            .Font.Size = 100
            .NumberFormat = "00"
            .Font.Bold = True
            .VerticalAlignment = xlCenter
            .HorizontalAlignment = xlCenter
        End With

        ' old location
        If Sheets("Bins").Range("E" & i) <> "" Then
            Sheets("Etikett").Range("K" & intRad + 1) = Sheets("Bins").Range("E" & i)
            With Sheets("Etikett").Range("K" & intRad + 1)
                .MergeCells = True
                .Font.Color = clr
                .Font.Size = 8
                .Font.Bold = True
                .VerticalAlignment = xlBottom
                .HorizontalAlignment = xlLeft
            End With
        End If

        ' copy already premade barcode or generate barcode if not premade
        If Sheets("Bins").Cells(i, 2) < 100 Then
            Sheets("0-99").Select
            shp = "B" & Right("0" & Sheets("Bins").Cells(i, 2), 2)
            Sheets("0-99").Shapes(shp).Select
        Else
            Sheets("VBA").Select
            ThisWorkbook.ActiveSheet.Shapes.SelectAll
            Selection.Delete

            Code128Generate_v2 30, 0, 40, 2.5, ThisWorkbook.ActiveSheet, Sheets("Bins").Cells(i, 2), 200
            ThisWorkbook.ActiveSheet.Shapes.SelectAll
            Selection.ShapeRange.Group.Select
        End If

        'color the barcode
        Selection.ShapeRange.Line.ForeColor.RGB = clr

        Selection.Copy
        Sheets("Etikett").Select
        Sheets("Etikett").Range("G" & intRad + 1 & ":J" & intRad + 1).MergeCells = True

        ' Set rowheights
        Sheets("Etikett").Rows(intRad).RowHeight = 135
        Sheets("Etikett").Rows(intRad + 1).RowHeight = 115
        If Etikettcount Mod 3 = 0 Then ' if it's the last label on paper, no space is needed between this and the next.
            Range("G" & intRad + 1).Select
            intRad = intRad - 1 
        Else
            Sheets("Etikett").Rows(intRad + 2).RowHeight = 25
            Range("G" & intRad + 1).Select
        End If
        ActiveSheet.Paste ' paste barcode

        Etikettcount = Etikettcount + 1
        intRad = intRad + 3
    End If
Next i

Keep in mind this is not all the code, but this is what copies the text and barcodes and places them on the sheet.

On my computer the output is as expected:
print output
enter image description here

On other computers the last character is slightly cut off and the vertical alignment is not correct.
As I wrote earlier I need the blank space between the lables to be at about 99 mm from the top and then 99 mm between them.

I have uploaded the full file if someone want to test it here: http://hoppvader.nu/docs/Streckkod.xlsm
Note that it's only module3 that is used, module2 is if you choose a checknumber "Checksiffra" other than 00-99.

Any help is appreciated on why it only works on my computer.

16
  • 2
    i didnt read the whole code but i ran your macro, i also got the same results of the last letters being cut off - until i changed the paper type from A4 to Letter. then it looked fine. i think that you accidentally wrote your code for a Letter aspect ratio (which is the default), and encountered an issue on the company computers where the paper type is already set to A4, since the issue happens on all computers (including mine) but not on yours, it logical to believe that the issue is on your computer.
    – Banana
    Mar 5, 2018 at 13:01
  • 1
    That is probably true. That the issue is on my computer, but the actual problem arises on other computers. So, change paper type to Letter and it will work? That should be able to be done with a macro to I think. Will look at it, Thanks!
    – Andreas
    Mar 5, 2018 at 13:04
  • 1
    @Banana I looked at it and it seems that is not the problem. It's set to A4, and if I change to 'Letter' it gets all screwed up. When I switch back to A4 my margins were reset to normal so I had to change them again to custom.
    – Andreas
    Mar 5, 2018 at 13:49
  • 2
    You don't set the font family in your code. Is possible by any chance that you have changed the default font in your excel and designed your table with the text and barcode with that, while everyone else is running with the original default font?
    – mshthn
    Mar 9, 2018 at 12:41
  • 2
    I agree with previous commentors, this is very likely a font/printer settings issue. You haven't shown us the code where you specify those settings, which makes me suspect that you are not explicitly setting all of them. When printing, you should definitely explicitly set the fonts, sizes and printer settings, otherwise it will default to whatever the user's defaults/last settings are. Mar 13, 2018 at 14:33

5 Answers 5

7
+100

The output can be affected by many things like the printer's resolution, the desktop's resolution, the font or the size of the cells.

For instance, when I draw a 10cm by 10cm square shape on a new sheet, the printed result is a 10.5cm x 9.5cm rectangle even though the scaling is disabled in the page setup and in the advanced options.

To get an accurate output, one solution would be to draw the content on a Chart Sheet since any drawing on this type of sheet is printed to the exact size provided in centimeter :

enter image description here

Here's an example to add a Chart sheet and to create the labels:

Sub DrawLabel()

  ' add new empty Chart sheet '
  Dim ch As Chart
  Set ch = ThisWorkbook.Charts.Add()
  ch.ChartArea.ClearContents
  ch.ChartArea.Format.Fill.Visible = msoFalse
  ch.ChartArea.Format.line.Visible = msoFalse

  ' setup page as A4 with no margin '
  ch.PageSetup.PaperSize = xlPaperA4
  ch.PageSetup.Orientation = xlPortrait
  ch.PageSetup.LeftMargin = 0
  ch.PageSetup.TopMargin = 0
  ch.PageSetup.RightMargin = 0
  ch.PageSetup.BottomMargin = 0
  ch.PageSetup.HeaderMargin = 0
  ch.PageSetup.FooterMargin = 0
  DoEvents ' force update '

  ' add labels
  AddText ch, x:=0.5, y:=0.5, w:=19.9, h:=4.6, Color:=vbRed, Border:=3, Size:=150, Text:="DB136C"
  AddText ch, x:=2.5, y:=5.1, w:=5, h:=4, Color:=vbRed, Border:=0, Size:=100, Text:="79"
  AddText ch, x:=0.5, y:=10, w:=19.9, h:=4.6, Color:=vbGreen, Border:=3, Size:=150, Text:="DB317A"
  AddText ch, x:=2.5, y:=14.6, w:=5, h:=4, Color:=vbGreen, Border:=0, Size:=100, Text:="35"
  AddText ch, x:=0.5, y:=19.5, w:=19.9, h:=4.6, Color:=vbBlack, Border:=3, Size:=150, Text:="AA102A"
  AddText ch, x:=2.5, y:=24.1, w:=5, h:=4, Color:=vbBlack, Border:=0, Size:=100, Text:="10"

End Sub

Private Sub AddText(self As Chart, x#, y#, w#, h#, Color&, Border#, Size#, Text$)
  With self.Shapes.AddTextBox( _
    msoTextOrientationHorizontal, _
    Application.CentimetersToPoints(x) - 8, _
    Application.CentimetersToPoints(y) - 8, _
    Application.CentimetersToPoints(w), _
    Application.CentimetersToPoints(h))

    .line.Weight = Border
    .line.ForeColor.RGB = Color
    .line.Visible = Border <> 0
    .TextFrame.VerticalAlignment = xlVAlignCenter
    .TextFrame.HorizontalAlignment = xlHAlignCenter
    .TextFrame2.TextRange.Font.Name = "Calibri"
    .TextFrame2.TextRange.Font.Size = Size
    .TextFrame2.TextRange.Font.Bold = msoTrue
    .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = Color
    .TextFrame2.TextRange.Text = Text
  End With
End Sub
0
2

Check the column width and compare your computers column widths with the others if they are different this is probably a font version issue:

  • Check which font you use in your sheet.
  • Check which version of the font is on your computer and which is on the other computer.
  • Additionally check the font versions of Calibri and Cambria on both computers (because these are Microsoft's defaults).

Make sure you install the same font version on all computers!

The issue here is probably that Excel determines the column width by the character width (see Description of how column widths are determined in Excel). So if the font changes the column width changes.

I had some issues like that a while ago when Microsoft Update delivered a wrong font file that had a different character width. If one of these wrong files is on yours or one of the others computer then the column width is calculated wrong.
Also see: Why is Excel column Pixel width different on different machines, but same OS, same resolution, same Excel verison, etc.?

5
  • 1
    Fonts are the same and I can't install fonts. See comment below question.
    – Andreas
    Mar 9, 2018 at 14:06
  • 2
    @Andreas Sorry, that I ask back but if you say fonts are the same did you check the version number of these fonts? I had the same fonts too but the version number of the fonts was not the same and that caused the issue in column width. And that you cannot install fonts doesn't mean that they are automatically on the same version number. For your interest: On my computer right side gets cut off (Excel 2016 x64 Win10 all latest build) C:\Windows\Fonts\Calibri\calibri.ttf Version 6.20
    – Pᴇʜ
    Mar 9, 2018 at 19:38
  • 1
    At least two computers have version 6.18 installed, meaning it's the same.
    – Andreas
    Mar 13, 2018 at 7:28
  • 1
    wow, did you change something on the uploaded file? Last time I tested your file it got definitely cut off. Now I tested (and downloaded) it again and it doesn't get cut off (everything fits). Identical computer, no updates yet, just 5 days later! Something odd is going on here.
    – Pᴇʜ
    Mar 13, 2018 at 7:30
  • 1
    That's what I'm saying. I have not done any changes to the file. Something is odd with Excel.
    – Andreas
    Mar 14, 2018 at 9:54
2

there should be an option when you go to print: "scale to fit" It might be in advanced options. On a mac I had to click "Show Details"

I used to so vba. And am a computer programmer. But the problem does not seem to be a code issue.

ps- you could probably find a way to enable "scale to fit" via macro. Here are some resources to check for a programming solution: https://www.ozgrid.com/forum/forum/help-forums/excel-general/5968-force-printing-macro-to-fit-page

https://www.experts-exchange.com/questions/28156905/VBA-Print-Code-Print-Area-Fit-on-one-page.html

excerpt from the above link solutioned by Patrick Matthews

 With Worksheets("name").PageSetup
   .Zoom = False
   .FitToPagesTall = 1
   .FitToPagesWide = 1
 End With

Responding to @Andreas, how about the code snippet?

additionally, remove the .FitToPagesTall

 With Worksheets("name").PageSetup
   .Zoom = False
   .FitToPagesWide = 1
 End With

hopefully then it will not align vertically but still align horizontally.

1
  • Scale to fit will not work. That is the same as shrinking it to fit the paper. This means the vertical alignment will be completly off.
    – Andreas
    Mar 13, 2018 at 7:23
0

It sounds like using the same file on a different computer is not your issue. The file is only one of many factors that affect a document's printed end-result.

  • Windows printer driver could be different versions on each of the computers (ie., was updated on one but not the other)

  • Windows printer settings could be slightly different between each of the computers.

I know you are adamant that the two computers are identical and that these settings cannot be changed, but difference like this on seemingly identical workstations happen all the time caused by any number of unexpected variables. (ie., "One time when a Windows Update was pushed to the two machines, one of them was accidentally powered off and didn't properly retrieve or install the update.")

There are a ton of buried printer settings and other variables that coulf be the potential cause of your issue, hidden away at various levels. (ie., system-level, device-level, application-level)


Below how to find three sets of properties that are likely the offender. Check all three places, from both computers, and compare the settings.

Device Manager

  • Hit the Windows KeyWindows Key, type device manger and push Enter

  • Double click Imaging Devices then right-click your desired printer and choose Properties

  • Click the Driver tab, and take note of the Driver Date and Driver Version

  • Repeat these steps on the other computer to compare the driver date & version.

If they do not match, make them match. If you don't have access to any of these areas, or are unsure which option to change, check with your I.T. department.

Printer Manufacturer Settings

  • Hit the Windows KeyWindows Key, type printers and push Enter

  • Right-click the desired printer and choose Printer Preferences

  • The layout of this window depends on your printer's manufacturer. Check through all values on all tabs, looking for differences between the two settings on the two machines.

Printer Properties

  • Go to Control PanelHardware and SoundDevices and Printers

  • Right-click the desired printer and choose Printer Properties

  • Check through all values on all tabs, looking for differences between the two settings on the two machines.

  • Finally, print a test page on the desired printer from both computers and inspect them closely for any differences (including version numbers).

1
  • To make sure printer and printer driver is not the problem I have tried printing to PDF also but that too makes a difference. One thing I did notice before going home yesterday was: the column width of the rightmost column (can't remember which one it is) had a column width of 8.46. if I changed it to 8 the width of the page fits but I still get the vertical alignment problem.
    – Andreas
    Mar 15, 2018 at 7:12
-1

I'd start by checking whether all print settings are set as desired (printers also have internal "default" print settings that can interfere with desired print). And if the font you're using is installed at work computers.

Then, I'd add following VBA code, to make sure the Excel print settings are the same on all computers (this is only to give you a hint and is a small portion of what can be set)

With Sheets("Etikett").PageSetup
    .PaperSize = xlPaperA4
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlPortrait
    .FitToPagesWide = 1
    .FitToPagesTall = 1
End With

And if the above does not help... Read below.

MS Office prints are a bit tricky thing. The problem is that Excel (and not only Excel) is not capable of generating print previews on its own, so what it does? It sends all the data to the printer (so it is the printer that does the job) and just "forwards" the results back. This is usually not a problem and goes unnoticed until someone tries to design pixel-perfect prints.

I had similar issue in my company, where we were using address labels for a post-mail stickers. After our printer broke down and we got ourselves a shiny new one (entirelly different model, brand etc) our templates were a mess and had to be re-aligned.

So all-in-all it could boil down to printer drivers...

6
  • I'll give it a try later, if I have time today. I saw a different answer or comment earlier that is now removed that was stating about the same as you. That it could be drivers and printers. It seems to hold some truth. If I print directly to another printer it gets missaligned. But if I print to PDF I get the result I want.
    – Andreas
    Mar 9, 2018 at 6:51
  • I am pretty sure that the code I posted should help. When I first opened your file, I got misaligned results. Upon applying my fix, labels seemed perfect. Do try it :)
    – Tomaski
    Mar 9, 2018 at 10:41
  • Sorry no. I didn't have time to try it on Friday and I started working again 10 minutes ago :-)
    – Andreas
    Mar 12, 2018 at 6:26
  • I tried this but it did not help completly. If I adjusted the margings slightly on the other computer it fit on the width, but it did not match up with the 99 mm.
    – Andreas
    Mar 13, 2018 at 7:11
  • @Andreas and if you skip the code that I have just posted and just adjust the margins to be 0 pt left and 0 pt right ? Alternatively replace my code with: Sheets("Etikett").PageSetup.LeftMargin = Application.InchesToPoints(0) Sheets("Etikett").PageSetup.RightMargin = Application.InchesToPoints(0)
    – Tomaski
    Mar 13, 2018 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.