行または列を参照する方法
行または列全体を操作するには、 Rows プロパティまたは Columns プロパティを使用します。 これらのプロパティは、セル範囲を表す Range オブジェクトを返します。 次の例では、 Rows(1)
Sheet1 で行 1 を返します。 その後、範囲の Font オブジェクトの Bold プロパティが True に設定されます。
Sub RowBold()
Worksheets("Sheet1").Rows(1).Font.Bold = True
End Sub
次に Rows プロパティ、または Columns プロパティを使った行および列の参照を示します。
Reference | 意味 |
---|---|
Rows(1) |
行 1 |
Rows |
ワークシートのすべての行 |
Columns(1) |
列 1 |
Columns("A") |
列 1 |
Columns |
ワークシートのすべての列 |
複数の行または列を同時に操作するには、オブジェクト型変数を宣言し、 Union メソッドを使って、複数の Rows プロパティまたは Columns プロパティの呼び出しを結合します。 次の使用例は、作業中のブックのシート 1 の行 1、3、および 5 の書式を太字に変更します。
Sub SeveralRows()
Worksheets("Sheet1").Activate
Dim myUnion As Range
Set myUnion = Union(Rows(1), Rows(3), Rows(5))
myUnion.Font.Bold = True
End Sub
提供されるサンプル コード: Dennis Wallentin、 VSTO & .NET & Excel この例では、選択した範囲から空の行を削除します。
Sub Delete_Empty_Rows()
'The range from which to delete the rows.
Dim rnSelection As Range
'Row and count variables used in the deletion process.
Dim lnLastRow As Long
Dim lnRowCount As Long
Dim lnDeletedRows As Long
'Initialize the number of deleted rows.
lnDeletedRows = 0
'Confirm that a range is selected, and that the range is contiguous.
If TypeName(Selection) = "Range" Then
If Selection.Areas.Count = 1 Then
'Initialize the range to what the user has selected, and initialize the count for the upcoming FOR loop.
Set rnSelection = Application.Selection
lnLastRow = rnSelection.Rows.Count
'Start at the bottom row and work up: if the row is empty then
'delete the row and increment the deleted row count.
For lnRowCount = lnLastRow To 1 Step -1
If Application.CountA(rnSelection.Rows(lnRowCount)) = 0 Then
rnSelection.Rows(lnRowCount).Delete
lnDeletedRows = lnDeletedRows + 1
End If
Next lnRowCount
rnSelection.Resize(lnLastRow - lnDeletedRows).Select
Else
MsgBox "Please select only one area.", vbInformation
End If
Else
MsgBox "Please select a range.", vbInformation
End If
'Turn screen updating back on.
Application.ScreenUpdating = True
End Sub
次の使用例は、選択された範囲から空の列を削除します。
Sub Delete_Empty_Columns()
'The range from which to delete the columns.
Dim rnSelection As Range
'Column and count variables used in the deletion process.
Dim lnLastColumn As Long
Dim lnColumnCount As Long
Dim lnDeletedColumns As Long
lnDeletedColumns = 0
'Confirm that a range is selected, and that the range is contiguous.
If TypeName(Selection) = "Range" Then
If Selection.Areas.Count = 1 Then
'Initialize the range to what the user has selected, and initialize the count for the upcoming FOR loop.
Set rnSelection = Application.Selection
lnLastColumn = rnSelection.Columns.Count
'Start at the far-right column and work left: if the column is empty then
'delete the column and increment the deleted column count.
For lnColumnCount = lnLastColumn To 1 Step -1
If Application.CountA(rnSelection.Columns(lnColumnCount)) = 0 Then
rnSelection.Columns(lnColumnCount).Delete
lnDeletedColumns = lnDeletedColumns + 1
End If
Next lnColumnCount
rnSelection.Resize(lnLastColumn - lnDeletedColumns).Select
Else
MsgBox "Please select only one area.", vbInformation
End If
Else
MsgBox "Please select a range.", vbInformation
End If
'Turn screen updating back on.
Application.ScreenUpdating = True
End Sub
投稿者について
Dennis Wallentin は、Excel および Excel Services の .NET Framework ソリューションを重点的に扱うブログである VSTO & .NET & Excel の作者です。 Dennis は 20 年以上 Excel ソリューションを開発しており、また『Professional Excel Development: The Definitive Guide to Developing Applications Using Microsoft Excel, VBA, and .NET (2nd Edition)』の共著者でもあります。
サポートとフィードバック
Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示