+ Reply to Thread
Results 1 to 9 of 9

Macros Button to Clear Rows of Data

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    62

    Macros Button to Clear Rows of Data

    I can't figure out how to add an error message to a button I have to clear rows of data.
    The code works, but I only want the user to be able to delete row ranges from 8 onward.

    If the user tries to delete any row range between 1:7 (1 through 7), then I want an error message to come up saying that "You can't delete any rows between 1 and 7."

    Any help would be greatly appreciated.

    Here's my code:

    Sub Clear_Row_Range()
    '
    ' Clear_Row_Range Macro
    '
    
    Dim MySelection As Range
        Dim msg As String, Title As String
        Dim Config As Integer, Ans As Integer
        msg = "Are you sure you want to clear this Row Range?"
        On Error Resume Next
        Title = "Confirm Row Deletion"
        Config = vbYesNo + vbExclamation
        Set MySelection = Application.InputBox(Prompt:="Enter the Row Range you would like to clear, separated with a colon                   (ex: 8:22)", Title:="Clear Row Range", Type:=8)
        MySelection.Select
        MyErr = Err
        On Error GoTo 0
        If MyErr <> 0 Then
            MsgBox "You Pressed Cancel. No Rows Were Cleared."
            
            Exit Sub
        End If
        
        'Can't delete rows 1-7 or error message pops up and exit sub
        If Config < 8 Then
        MsgBox ("Rows 1-7 cannot be cleared. Please enter row numbers from 8 and above."), vbInformation
        Exit Sub
        End If
        
        Ans = MsgBox(msg, Config, Title)
        If Ans = vbYes Then
            MySelection.EntireRow.ClearContents
            MsgBox ("Your Rows were successfully cleared."), vbInformation
        Else
            Range("A8").End(xlDown).Offset(1, 0).Select
        End If
        
    '
    End Sub


    This is my feeble attempt to add the error message, but it doesn't work. It's hard for me to figure out because the user enters the data in row ranges such as: "8:22" .

     'Can't delete rows 1-7 or error message pops up and exit sub
        If Config < 8 Then
        MsgBox ("Rows 1-7 cannot be cleared. Please enter row numbers from 8 and above."), vbInformation
        Exit Sub
        End If

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macros Button to Clear Rows of Data

    Hi, lottidotti,

    check the row number for the first cell of the selected area:
    '...
        Set MySelection = Application.InputBox(Prompt:="Enter the Row Range you would like to clear, separated with a colon                   (ex: 8:22)", Title:="Clear Row Range", Type:=8)
        If MySelection.Cells(1).Row < 8 Then
          MsgBox "Your Message to User"
          Exit Sub
        End If
    '...
    You need to check Abort prior to this check for ending.

    Ciao,
    Holger

  3. #3
    Registered User
    Join Date
    08-19-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Macros Button to Clear Rows of Data

    Thank you Holger,
    It worked perfectly!

    I have another worksheet that I need to do the same thing with (post error message if the user tries to delete rows 1-7). However, this code has the user input individual rows that are comma separated like this (8,11,23).

    Any ideas on how to do the same thing with this code?

    Sub Clear_Individual_Rows()
    '
    ' Clear_Individual_Rows Macro
    '
    Dim RowsToClear    As String
        Dim x, i As Long
        
        RowsToClear = Application.InputBox("Enter the Rows to Clear separated by comma", "Clear Rows", "8,11,23", Type:=1 + 2)
        
        If RowsToClear = "False" Then Exit Sub
        
        If MsgBox("Are you sure you want to Clear these ROWS " & vbLf & RowsToClear & " ?", vbYesNo + vbInformation) = vbYes Then
            x = Split(RowsToClear, ",")
            On Error Resume Next
            For i = 0 To UBound(x)
                Application.Intersect(Range("a:bg"), Rows(x(i))).ClearContents
            Next
            On Error GoTo 0
        End If
    '
    End Sub

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macros Button to Clear Rows of Data

    Hi, lottidotti,

    why change from Type:=8 (Range) to 1+2? If you use Type:=8 and mark the rows you could easily work on that.

    The solution may either be use Type:=8 for Application.InputBox or split up the rows delivered from the string and check for their numbers in the array or double the row numbers for the string like ("8:8, 11:11, 21:21"). The split is used later on for clearing contents in your code.

    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    08-19-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Macros Button to Clear Rows of Data

    Do you know of a way that I could search the string for the text or numbers "1,2,3,4,5,6,7" without it confusing it with 11,12,13, etc.

    I saw that someone else had some code like this checking for specific text within a string. (See below)

    Sub stringtest() 
        Dim str1 As String, str2 As String 
         
        str1 = InputBox("Enter something") 
        str2 = InputBox("Enter something else") 
         
        If InStr(1, str1, str2, vbTextCompare) Then 
            MsgBox "Match Found" 
        Else 
            MsgBox "No Match Found" 
        End If 
         
         
    End Sub

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macros Button to Clear Rows of Data

    Hi, lottidotti,

    why donīt you use the approach of splitting the string?

    I had to work a bit on the code as I use a non-englisch version of Excel:

    Sub Clear_Individual_Rows()
    '
    ' Clear_Individual_Rows Macro
    '
    Dim varRowsToClear    As Variant
    Dim varArray As Variant
    Dim lngCounter As Long
        
        varRowsToClear = Application.InputBox("Enter the Rows to Clear separated by comma", "Clear Rows", "8,11,23", Type:=1 + 2)
        
        If varRowsToClear = False Then Exit Sub
        varArray = Split(varRowsToClear, ",")
        For lngCounter = 0 To UBound(varArray)
            If Val(varArray(lngCounter)) < 8 Then
                MsgBox "Wrong line detected", vbExclamation, "Macro aborted"
                Exit Sub
            End If
        Next lngCounter
        
        If MsgBox("Are you sure you want to Clear these ROWS " & vbLf & varRowsToClear & " ?", vbYesNo + vbInformation) = vbYes Then
            varArray = Split(varRowsToClear, ",")
            On Error Resume Next
            For lngCounter = 0 To UBound(varArray)
                Application.Intersect(Range("a:bg"), Rows(varArray(lngCounter))).ClearContents
            Next lngCounter
            On Error GoTo 0
        End If
    '
    End Sub
    Maybe another idea could be to lock rows 1 to 7 and use a code like
    With ActiveSheet
      .EnableSelection = xlUnlockedCells
      .Protect contents:=True, UserInterfaceOnly:=True
    End With
    to avoid Rows 1:7 to be selected at all.

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    08-19-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Macros Button to Clear Rows of Data

    Hi Holger,
    The user requires the 1+2 rather than the Type:=8 for the other worksheet because they have a huge amount of data that needs to stay in order, but only have a row deleted here and there without disturbing the rest of the worksheet.

    Anyways, I need to keep it with the user entering rows to delete as (8,12,51,127,etc.)
    I can't do the 8:8, 11:11, split method.
    So I'm not quite sure how to adjust my code so they can't delete rows 1-7.
    Thanks for your help.
    I think I'll be pulling out my hair for a while with this one!

    ---------- Post added at 09:06 AM ---------- Previous post was at 08:57 AM ----------

    Do you know of a way that I could search the string for the text or numbers "1,2,3,4,5,6,7" without it confusing it with 11,12,13, etc.

    I saw that someone else had some code like this checking for specific text within a string. (See below)

    Sub stringtest() 
        Dim str1 As String, str2 As String 
         
        str1 = InputBox("Enter something") 
        str2 = InputBox("Enter something else") 
         
        If InStr(1, str1, str2, vbTextCompare) Then 
            MsgBox "Match Found" 
        Else 
            MsgBox "No Match Found" 
        End If 
         
         
    End Sub

  8. #8
    Registered User
    Join Date
    08-19-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Macros Button to Clear Rows of Data

    Hi Holger,
    I used your split code and it worked great!
    Thank you so much for helping me.
    I'm a newbie, so I didn't know how to write the split code.
    You saved me so many headaches!
    Cheers!

  9. #9
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Macros Button to Clear Rows of Data

    @ lottidotti

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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