ExcelのVBA・マクロでセルの文字を中央揃えに設定・変更する方法

ここでは、ExcelのVBA・マクロでセルの文字を中央揃えに設定・変更する方法を紹介します。

ExcelのVBA・マクロでセルの文字を中央揃えに設定・変更するには、HorizontalAlignment プロパティを変更します。

Sub test20()
Range(“A1”).Value = “グループ”
Range(“B1”).Value = “名前”
Range(“C1”).Value = “値”
Range(“A2”).Value = “A”
Range(“A3”).Value = “B”
Range(“A4”).Value = “C”
Range(“A5”).Value = “A”
Range(“A6”).Value = “B”
Range(“A7”).Value = “C”
Range(“B2”).Value = “高橋”
Range(“B3”).Value = “鈴木”
Range(“B4”).Value = “田中”
Range(“B5”).Value = “松井”
Range(“B6”).Value = “野村”
Range(“B7”).Value = “谷重”
Range(“C2:C7”).Value = 100

Range(“A1:C1”).Interior.Color = rgbYellow
Range(“A1:C1”).Font.Color = rgbRed
Range(“A1:C1”).HorizontalAlignment = xlCenter
Range(“A1:C7”).Borders.Weight = xlThin
Range(“A1:C7”).Font.Name = “Meiryo UI”
Range(“A1:C7”).Font.Size = 12

End Sub

ここでは、「Range(“A1:C1”).HorizontalAlignment = xlCenter」で、A1からC1までのセルの文字を中央揃えしています。左揃え、右揃えの場合は、設定する値をxlLeft、xlRightにそれぞれ変更します。