K1 零基础学习VBA -- 单元格格式

0 阅读1分钟

一、数字格式

控制数字显示格式,不改变原值

Range("A1").NumberFormat = "General"  
Range("A1").NumberFormat = "@"
Range("A1").NumberFormat = "0.00" / "#.##" / "#,##0.00"
Range("A1").NumberFormat = "0.00%" 
Range("A1").NumberFormat = "yyyy-mm-dd" 
Range("A1").NumberFormat = "¥#,##0.00"

二、字体格式

Range("A1").Font.Color = RGB(255,0,0)  'RGB调色,推荐
Range("A1").Font.Color = vbRed   '内置颜色
Range("A1").Font.Color = RGB(0,0,0)    '清除颜色

三、填充背景色

Range("A1:C10").Interior.Color = RGB(255, 215, 0)
Range("A1").Font.Color = vbRed   '内置颜色

四、边框

'设置外部框线
With sht.Range("A1:K10").Borders 
    .LineStyle = xlContinuous '实线 
    .Weight = xlThin '细线 
    .Color = RGB(0, 0, 0) '黑色边框 
End With

'其他设置
.Borders(xlEdgeLeft | xlEdgeTop | xlInsideHorizontal) 
.linestyle = xlContinuous | xlDot | xlDash | xlDashDot | xlLineStyleNone等

.weight = xlThin | xlMedium | xlThick
.color = RGB(0, 0, 0)

五、对齐方式

Range("A1:C10").Alignment.HorizontalAlignment = xlCenter | xlJustify
Range("A1:C10").Alignment.VerticalAlignment = xlCenter | xlJustify

六、单元格保护属性

七、行高列宽

  • 行高:Rows().RowHeight

  • 列宽:Columns().ColumnWidth

八、条件格式