How to detect if the cell being edited belongs to the last row of a defined name range

后端 未结 1 1254
说谎
说谎 2021-01-27 00:59

I have a defined name call \"Input_Range\". If a last empty row of the input range is being edited (add value to an empty row), then insert an empty row, automatically.

相关标签:
1条回答
  • 2021-01-27 01:24

    Is this what you are trying?

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim MRange As Range, rng As Range
    
        On Error GoTo Whoa
    
        Set MRange = Range("InputRange")
    
        '~~> Get the last Row of the range
        Set rng = MRange.Range(MRange.Cells(MRange.Rows.Count, 1), _
        MRange.Cells(MRange.Rows.Count, MRange.Columns.Count))
    
        '~~> Trap changes in the last row of the range
        If Not Intersect(Target, rng) Is Nothing Then
            'If Application.WorksheetFunction.CountA(rng) > 0 Then
                'Application.EnableEvents = False
    
                'rng.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            'End If
        End If
    
    LetsContinue:
        Application.EnableEvents = True
        Exit Sub
    Whoa:
        MsgBox Err.Description
        Resume LetsContinue
    End Sub
    
    0 讨论(0)
提交回复
热议问题