萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 辦公室軟體 >  >> Excel 教學 >> Excel 綜合

Excel VBA 儲存格對齊屬性:HorizontalAlignment 與 VerticalAlignment 常數完整表

控制儲存格水平與垂直對齊,透過 Range.HorizontalAlignment 與 VerticalAlignment 屬性設定,使用 xl* 常數。

水平對齊常數

常數 值 對齊方式 說明
xlGeneral1標準預設,數字靠右、文字靠左
xlLeft-4131靠左
xlCenter-4108置中
xlRight-4152靠右
xlFill5填滿重複文字填滿欄寬
xlJustify-4130分散對齊文字兩端對齊(多行時)
xlCenterAcrossSelection7跨欄置中不合併儲存格,視覺置中於選取範圍
xlDistributed-4117分散對齊字元間距均勻分布(含單行)

垂直對齊常數

常數 值 對齊方式 說明
xlTop-4160靠上
xlCenter-4108置中預設
xlBottom-4107靠下
xlFill5填滿垂直重複文字
xlJustify-4130兩端對齊多行文字上下對齊
xlDistributed-4117分散對齊行距均勻分布

程式碼範例

範例 1:單一儲存格設定

Dim rng As Range
Set rng = Range("A1")
rng.HorizontalAlignment = xlCenter
rng.VerticalAlignment = xlTop

範例 2:矩陣式示範各種對齊組合

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 避免自動換行干擾。
copyright © 萬盛學電腦網 all rights reserved