エクセルマクロVBA:セルの操作
エクセルのちょっとしたテクニックを紹介します。
セルの列幅や行の高さを入力されている文字列に合わせて調節できます。
・列幅を調節する場合
Range("A1").EntireColumn.AutoFitまたは
Columns(1).AutoFit
・行の高さを調整する場合
Range("A1").EntireRow.AutoFitまたは
Rows(1).AutoFit
以下のサンプルコードで試してみてくださいね。
Sub Test2()
With Range("A1")
.Value = "あっという間にセルの列幅や行の高さが変わります。"
.RowHeight = 30
.ColumnWidth = 30
End With
MsgBox "列幅を調節"
Range("A1").EntireColumn.AutoFit
MsgBox "行の高さを調節"
Range("A1").EntireRow.AutoFit
With Range("A1")
.RowHeight = 30
.ColumnWidth = 30
End With
MsgBox "列幅を調節"
Columns(1).AutoFit
MsgBox "行の高さを調節"
Rows(1).AutoFit
End Sub
セルの列幅や行の高さを入力されている文字列に合わせて調節できます。
・列幅を調節する場合
Range("A1").EntireColumn.AutoFitまたは
Columns(1).AutoFit
・行の高さを調整する場合
Range("A1").EntireRow.AutoFitまたは
Rows(1).AutoFit
以下のサンプルコードで試してみてくださいね。
Sub Test2()
With Range("A1")
.Value = "あっという間にセルの列幅や行の高さが変わります。"
.RowHeight = 30
.ColumnWidth = 30
End With
MsgBox "列幅を調節"
Range("A1").EntireColumn.AutoFit
MsgBox "行の高さを調節"
Range("A1").EntireRow.AutoFit
With Range("A1")
.RowHeight = 30
.ColumnWidth = 30
End With
MsgBox "列幅を調節"
Columns(1).AutoFit
MsgBox "行の高さを調節"
Rows(1).AutoFit
End Sub
トラックバックURL
この記事にコメントする