VBA碎片之调整Excel格式

编程

Sub 格式调整()

Dim row_cell As Long, column_cell As Long
row_cell = ActiveSheet.UsedRange.Rows.Count
column_cell = ActiveSheet.UsedRange.Columns.Count
Debug.Print (row_cell)
Debug.Print (column_cell)
Range("a1:" & Cells(row_cell, column_cell).Address).Select
'下面这句可以根据列号数字,返回对应的列号字母
'MsgBox (Cells(row_cell, column_cell).Address)
With Selection.Font
    .Name = "微软雅黑"
    .Size = 10
End With
'调整上下对齐,居中对齐
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With
'全部边框
With Selection.Borders
    .LineStyle = xlContinuous
    '设置边框颜色~
    '.ColorIndex = 39
End With
'全部选中
Cells.Select
'全部自适应对齐宽度
'Cells.EntireColumn.AutoFit
'因自适应宽度有时候不是我们想要的,所以还是设置固定的把~~列宽10(85像素,约0.13宽=1像素),行高21(28像素 约0.75高=1像素)
Selection.RowHeight = 20
Selection.ColumnWidth = 10
Range("a1").Select
'设置自动筛选  因为Selection.AutoFilter可以增加筛选,也可以取消筛选,所以只好用activesheet.autofiltermode来进行判断,它是一个布尔值,如果没有筛选,就加一个筛选上去~ 要注意,一个是selection开始,一个是activesheet开始
If ActiveSheet.AutoFilterMode = False Then
    Selection.AutoFilter
End If

' '如下是拓展知识~ 选中a1,ctrl + 下箭头,ctrl + 右箭头
' Range("A1").Select
' Range(Selection, Selection.End(xlDown)).Select
' Range(Selection, Selection.End(xlToRight)).Select
' '清除全部,清除格式,清除内容,清除批注,清除超链接
' Selection.Clear
' Selection.ClearFormats
' Selection.ClearContents
' Selection.ClearComments
' Selection.ClearHyperlinks
' 冻结首行~ 如果activewindow.freezepanes = false 就代变现在是没有冻结窗口的状态,然后让它冻结首行~

Debug.Print (ActiveWindow.FreezePanes)  'debug.print 可以在立即窗口里面看到结果,方便调试代码~
If Not ActiveWindow.FreezePanes Then
    With ActiveWindow
        .SplitColumn = 0
        .SplitRow = 1
    End With
    ActiveWindow.FreezePanes = True
End If

End Sub

标签: vba

+0 +270
新评论

请填写评论信息

暂无评论