

Excel VBA
Excel vba设置行高及自动行高
2017-10-09 15:17:08
在Excel工作表中,有时会因为文本内容的长度影响阅读。需要调整行高来显示全部信息。
excel对单元格设置行高很简单。一般情况下,都是通过点击菜单:格式——行——行高,然后进行设置。
这里我们介绍一下用Excel vba来设置Excel工作表的行高
1、建立基础数据表及两个按钮
2、为按钮添加代码
设置行高的代码:
Sub 设置行高()
Dim h As Long, r As Long, i As Integer, n As Integer
Dim ws1 As Worksheet
h = Application.InputBox(prompt:="请输入所选行的高度:", Title:="输入行高", Type:=1)
Set ws1 = ActiveSheet
n = Selection.Rows.Count
r = ActiveCell.Row
For i = 1 To n
ws1.Rows(r + i - 1).RowHeight = h
Next
Set ws1 = Nothing
End Sub自动行高代码:Sub 自动调整行高() Selection.Rows.AutoFitEnd Sub3、运行效果图