VBA实用小程序73:厘米、英寸和像素、磅的转换

2021-05-07 10:37:06 浏览数 (1)

excelperfect

Excel、PowerPoint和Word使用了一种称为磅的测量方法。但是,标准应用程序菜单中的测量单位是厘米或英寸。

可以在Excel选项更改默认的测量单位。单击“文件->选项->高级->显示->标尺单位”,如下图1所示,修改默认单位。

图1

英寸、厘米、磅的转换

从英寸或厘米转换为磅相当简单,因为1英寸等于72磅,1厘米等于28.35磅(四舍五入到小数点后2位)。Microsoft提供了两个有用的VBA函数来进行转换。

从英寸转换为磅

Sub InchesToPoints()

Dim valueInches As Double

Dim valuePoints As Double

valueInches = 25

valuePoints =Application.InchesToPoints(valueInches)

Debug.Print valuePoints

End Sub

从厘米转换为磅

Sub CentimetersToPoints()

Dim valueCentimeters As Double

Dim valuePoints As Double

valueCentimeters = 25

valuePoints =Application.CentimetersToPoints(valueCentimeters)

Debug.Print valuePoints

End Sub

从磅转换为厘米

Sub PointsToCentimeters()

Dim valuePoints As Double

Dim valueCentimeters As Double

valuePoints = 50

valueCentimeters = valuePoints /Application.CentimetersToPoints(1)

Debug.Print valueCentimeters

End Sub

从磅转换为英寸

Sub PointsToInches()

Dim valuePoints As Double

Dim valueInches As Double

valuePoints = 700

valueInches = valuePoints /Application.CentimetersToPoints(1)

Debug.Print valueInches

End Sub

磅、像素的转换

虽然像素似乎是一个可以理解的度量单位,用于控制对象的位置,但它并不像预期的那样有用。像素的数量取决于各种因素,例如每个监视器使用的屏幕分辨率。可以使用下面的VBA代码将磅转换为像素。

Sub PointsToPixels()

Dim valuePoints As Long

Dim valuePixels As Long

valuePoints = 500

valuePixels =Application.ActiveWindow.PointsToScreenPixelsX(valuePoints)

Debug.Print "X轴像素: " &valuePixels

valuePixels = Application.ActiveWindow.PointsToScreenPixelsY(valuePoints)

Debug.Print "Y轴像素: " &valuePixels

End Sub

注:本文的程序整理自exceloffthegrid.com,供学习参考。

欢迎在下面留言,完善本文内容,让更多的人学到更完美的知识。

vba

0 人点赞