控制儲存格水平與垂直對齊,透過 Range.HorizontalAlignment 與 VerticalAlignment 屬性設定,使用 xl* 常數。
| 常數 | 值 | 對齊方式 | 說明 |
|---|---|---|---|
xlGeneral | 1 | 標準 | 預設,數字靠右、文字靠左 |
xlLeft | -4131 | 靠左 | |
xlCenter | -4108 | 置中 | |
xlRight | -4152 | 靠右 | |
xlFill | 5 | 填滿 | 重複文字填滿欄寬 |
xlJustify | -4130 | 分散對齊 | 文字兩端對齊(多行時) |
xlCenterAcrossSelection | 7 | 跨欄置中 | 不合併儲存格,視覺置中於選取範圍 |
xlDistributed | -4117 | 分散對齊 | 字元間距均勻分布(含單行) |
| 常數 | 值 | 對齊方式 | 說明 |
|---|---|---|---|
xlTop | -4160 | 靠上 | |
xlCenter | -4108 | 置中 | 預設 |
xlBottom | -4107 | 靠下 | |
xlFill | 5 | 填滿 | 垂直重複文字 |
xlJustify | -4130 | 兩端對齊 | 多行文字上下對齊 |
xlDistributed | -4117 | 分散對齊 | 行距均勻分布 |
Dim rng As Range
Set rng = Range("A1")
rng.HorizontalAlignment = xlCenter
rng.VerticalAlignment = xlTop
Range("A1:C4").Value = "文言文"
Range("A1:C1").HorizontalAlignment = xlLeft
Range("A2:C2").HorizontalAlignment = xlCenter
Range("A3:C3").HorizontalAlignment = xlRight
Range("A4:C4").HorizontalAlignment = xlDistributed
Range("A1:A4").VerticalAlignment = xlTop
Range("B1:B4").VerticalAlignment = xlCenter
Range("C1:C4").VerticalAlignment = xlBottom
執行後 A1:C4 形成 3×4 矩陣,每列不同水平對齊、每行不同垂直對齊,直觀展示所有組合效果。
xlCenterAcrossSelection 為「跨欄置中」替代方案,避免合併儲存格造成排序/篩選問題。xlDistributed(分散對齊)需儲存格寬度足夠才明顯。Range.WrapText = False 避免自動換行干擾。