|
楼主 |
发表于 2013-10-18 16:21
|
显示全部楼层
本帖最后由 cbtaja 于 2013-10-20 14:29 编辑
关于以厘米为单位来设置列宽的问题,此前的代码中先把厘米转换为英寸,然后把英寸按固定换算关系转换磅(为1英寸=72磅),相对比较麻烦,现直接用Application.CentimetersToPoints 方法()方法来获得。因此,一楼中的代码宜更新为如下:
- '-------------------
- Private Sub 恢复列宽()
- '本代码调用Std_Add()、cmColumnWidth()函数,
- '为第1至9列分别设置以厘米为度量单位的列宽
- Dim std_fjz As Variant, i As Long, a As Boolean
- Dim arr
- std_fjz = Std_Add
- Application.ScreenUpdating = False
- arr = Split(",1.5,1.8,2.6,2.8,2.54,0.69,1.8,7.01,1.6", ",")
- For i = 1 To UBound(arr)
- a = lmlk(Cells(37, i), arr(i), std_fjz(0), std_fjz(1))
- Next
- End Sub
- '-------------------
- Private Function Std_Add()
- Dim a1 As Double, a2 As Double, std As Double, fjz As Double, Orgnl As Double
- Application.ScreenUpdating = False
- With ActiveSheet.Cells.SpecialCells(xlCellTypeVisible).Cells(1)
- Orgnl = .ColumnWidth
- .ColumnWidth = 2: a1 = .Width
- .ColumnWidth = 3: a2 = .Width
- .ColumnWidth = Orgnl
- std = a2 - a1 '1个标准字符宽度的磅值
- fjz = a1 - 2 * std '每个单元格附加宽度的磅值
- Std_Add = Array(std, fjz)
- End With
- End Function
- '--------------------
- Private Function cmColumnWidth(ByRef rng As Range, ByVal lmz_Width As Double, _
- ByVal std As Double, ByVal fjz As Double) As Boolean
- '共4个参数,参数1为单元格区域,必须是Range对象;参数2为需要设定的列宽厘米值;
- '参数3为当前表格1个标准字符宽度的磅值,参数4为每个单元格中附加的固定磅数
- '参数3和参数4均与当前表格字体相关,需要通过额外测试、计算才能得到。见Std_Add自定义函数。
- Dim a1, a2, sjlmz_width, zfs, cz, Cm2Points As Double
- With Application
- .ScreenUpdating = False
- Cms2Points = .CentimetersToPoints(1)
- End With
- With rng.Columns.Cells(1)
- zfs = (lmz_Width / Cms2Points - fjz) / std '计算出的理论宽度(字符数)
- .ColumnWidth = zfs
- sjlmz_width = Round(.Width / Cms2Points, 4)
- cz = Round(sjlmz_width - lmz_Width, 4) '厘米
- ' .Value = sjlmz_width & "厘米"
- lmlk = (Abs(cz) < 0.0127) '即1/200 英寸,如果为True,则表格线精度为200dpi以上
- ' .Offset(1, 0) = lmlk1
- End With
- End Function
- '---------------------
- Sub cm_RowHight()
- '本程序为选定行指定以厘米为单位的行高
- Dim k As Single, wd As Single, Cm2Points As Double
- If TypeName(Selection) <> "Range" Then Exit Sub
- With Application
- .ScreenUpdating = False
- Cm2Points = .CentimetersToPoints(1)
- wd = .InputBox("请为已经选择的区域设定行高(单位:厘米):", "指定行高", 2, Type:=1)
- End with
- Selection.RowHeight = Round(wd / Cm2Points * 4, 0) / 4
- End Sub
复制代码 |
|