gpt4 book ai didi

vba - 同一个文件在两台不同的计算机上怎么会得到不同的结果呢?

转载 作者:行者123 更新时间:2023-12-01 17:41:39 27 4
gpt4 key购买 nike

我制作了一个 VBA 脚本,它将从一张纸中读取值并在另一张纸上创建“标签”。
该标签应该打印在分为三部分的特殊纸张上。

由于我住在瑞典,我们使用 A4 纸张尺寸(297x210 毫米)。标签尺寸应为 99x210 毫米。
这意味着每个值都需要打印在纸张上的确切位置。

我为我的公司这样做,因此所有计算机都完全相同。
相同型号、相同版本的 Windows、相同版本的 Excel。

这是代码的一小部分(与文本定位相关)

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

请记住,这不是全部代码,而是复制文本和条形码并将其放置在工作表上的代码。

在我的计算机上,输出符合预期:
打印输出
enter image description here

在其他计算机上,最后一个字符会被稍微切断,并且垂直对齐不正确。
正如我之前所写,我需要标签之间的空白距离顶部约 99 毫米,标签之间的距离为 99 毫米。

如果有人想在这里测试它,我已经上传了完整的文件:http://hoppvader.nu/docs/Streckkod.xlsm
请注意,仅使用 module3,如果您选择除 00-99 之外的校验码“Checksiffra”,则使用 module2。

任何有关为什么它只能在我的计算机上运行的帮助都将不胜感激。

最佳答案

输出可能会受到许多因素的影响,例如打印机分辨率、桌面分辨率、字体或单元格大小。

例如,当我在新纸张上绘制 10 厘米 x 10 厘米的正方形形状时,即使在页面设置和高级选项中禁用缩放,打印结果也是 10.5 厘米 x 9.5 厘米的矩形。

为了获得准确的输出,一种解决方案是在图表表上绘制内容,因为此类图表上的任何绘图都会打印为以厘米为单位提供的精确尺寸:

enter image description here

以下是添加图表工作表并创建标签的示例:

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

关于vba - 同一个文件在两台不同的计算机上怎么会得到不同的结果呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49108068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com