+ Reply to Thread
Results 1 to 4 of 4

Macro to delete row within a specified range or disallow if out of range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Smile Macro to delete row within a specified range or disallow if out of range

    I’m trying to write a macro that deletes the row of the active cell but if the selected cell is out of a specified range a message is displayed.
    Any help is appreciated, the code I have so far is below.

    If ActiveCell.Range("A13, C12") Then
    ActiveCell.EntireRow.Delete
    Else
    MsgBox "You must select a cell within the range A13 to C12"
    End If
    Last edited by Mayweed; 04-07-2011 at 01:51 PM.

  2. #2
    Valued Forum Contributor jwright650's Avatar
    Join Date
    12-10-2010
    Location
    Va, USA
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    606

    Re: Macro to delete row within a specified range or disallow if out of range

    Will it hurt anything if the user ignores the msgbox and continues anyway?
    Life is like a roll of toilet paper. The closer it gets to the end, the faster it goes.
    John Wright

  3. #3
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Macro to delete row within a specified range or disallow if out of range

    As long as the user can only delete a row within the specified range (designated by selecting a cell in the desired row), the MsgBox can be ignored.

  4. #4
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Smile Re: Macro to delete row within a specified range or disallow if out of range

    If I totally deny reading a post by shg concerning ws selectionChange events on another forum, I can honestly say that I've solved this one myself!

    The only caveat that I'm aware of; if using this code on a protected worksheet, then cell B1 must be unlocked.
    If it helps anyone, I've attached a workbook.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim myrange As Range
        On Error Resume Next
        Set myrange = Intersect(Range("A2:C20"), Target)
        If Not myrange Is Nothing Then
             Range("B1") = "True"
             Else
             Range("B1") = "False"
        End If
    End Sub
    
    
    Sub DelRowProtected()
    ActiveSheet.Unprotect Password:=""
    If Range("B1") = "True" Then
    ActiveCell.EntireRow.Delete
    Else
    MsgBox "Select a cell in the range A2 to C20 to delete the row"
    End If
    ActiveSheet.Protect Password:=""
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1