Range プロパティ (Excel)Range.Row property (Excel)

範囲内の最初の領域の最初の行の番号を返します。Returns the number of the first row of the first area in the range. 取得のみ可能な Long 値です。Read-only Long.

構文Syntax

expression.Row

expressionRange オブジェクトを表す変数です。expression A variable that represents a Range object.

Example

次の使用例は、シート1のすべての行の高さを 4ポイントに設定します。This example sets the row height of every other row on Sheet1 to 4 points.

For Each rw In Worksheets("Sheet1").Rows 
    If rw.Row Mod 2 = 0 Then 
        rw.RowHeight = 4 
    End If 
Next rw

次の使用例は、 Beforedoubleclickワークシートのイベントを使用して、あるワークシートから別のワークシートにデータの行をコピーします。This example uses the BeforeDoubleClick worksheet event to copy a row of data from one worksheet to another. このコードを実行するには、対象のワークシートの名前が列 A にある必要があります。次の使用例は、データが含まれているセルをダブルクリックすると、移動先のワークシートの名前を列 A から取得し、データの行全体を対象のワークシートの次の使用可能な行にコピーします。To run this code, the name of the target worksheet must be in column A. When you double-click a cell that contains data, this example gets the target worksheet name from column A and copies the entire row of data into the next available row on the target worksheet. この例では、 Targetキーワードを使用してアクティブな行にアクセスします。This example accesses the active row by using the Target keyword.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    'If the double click occurs on the header row or an empty cell, exit the macro.
    If Target.Row = 1 Then Exit Sub
    If Target.Row > ActiveSheet.UsedRange.Rows.Count Then Exit Sub
    If Target.Column > ActiveSheet.UsedRange.Columns.Count Then Exit Sub
    
    'Override the default double-click behavior with this function.
    Cancel = True
    
    'Declare your variables.
    Dim wks As Worksheet, xRow As Long
    
    'If an error occurs, use inline error handling.
    On Error Resume Next
    
    'Set the target worksheet as the worksheet whose name is listed in the first cell of the current row.
    Set wks = Worksheets(CStr(Cells(Target.Row, 1).Value))
    'If there is an error, exit the macro.
    If Err > 0 Then
        Err.Clear
        Exit Sub
    'Otherwise, find the next empty row in the target worksheet and copy the data into that row.
    Else
        xRow = wks.Cells(wks.Rows.Count, 1).End(xlUp).Row + 1
        wks.Range(wks.Cells(xRow, 1), wks.Cells(xRow, 7)).Value = _
        Range(Cells(Target.Row, 1), Cells(Target.Row, 7)).Value
    End If
End Sub

次の使用例は、選択された範囲から空の行を削除します。This example deletes the empty rows from a selected range.

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

サポートとフィードバックSupport and feedback

Office VBA またはこの説明書に関するご質問やフィードバックがありますか?Have questions or feedback about Office VBA or this documentation? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.