Looping off user input

Excel Ent

New Member
Joined
Aug 21, 2019
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
Hello folks
I have a project workbook that takes the users input from sheet 1 and compares it against a range in sheet 2 ("Jan") If the match is found on this sheet ("Jan"), I need it to clear the row that the match is found on while preserving the formulas in "A" and "B2". So for example, if Jon is found in "A7", cells "A7" and "B7" are cleared but still hold the formula while cells "C" thru "G" are cleared as well. (cells C thru G are just normal cells). The highlighted text below keeps giving me a runtime error. One last level of complexity, if this can work, I need it to look through the other 11 worksheets "Feb" thru "Dec" and do the same. Thanks in advance!


Do
Ans = InputBox("Enter the name of the associate you wish to remove.")
If Ans = "" Then Exit Sub
If Application.CountIf(Range("A1:A358"), Ans) > 0 Then
Sheets("Jan").Select
Flg = True
For rw = 358 To 3 Step -1
If Cells(rw, "A") = Ans Then Rows(rw).Cells.SpecialCells(xlCellTypeConstants).ClearContents
Next rw
End If
If Not Flg Then If MsgBox("That entry is invalid. Do you want to try again?", vbYesNo) = vbNo Then Exit Sub
Loop Until Flg
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Seems to work ok if your sheet(s) have data in them

Try this update

VBA Code:
Sub ClearText()
    Dim wsInput     As Worksheet, sh As Worksheet
    Dim Ans         As Variant
    Dim rw          As Long
    Dim i           As Integer
    
    Set wsInput = Worksheets("Inputs")
    
    Do
        Ans = InputBox("Enter the name of the associate you wish To remove.", "Enter Search Text")
        'cancel pressed
        If StrPtr(Ans) = 0 Then Exit Sub
        If Len(Ans) > 0 Then
            If Application.CountIf(wsInput.Range("A1:A358"), Ans) > 0 Then
                Application.EnableEvents = False
                For i = 1 To 12
                    Set sh = Worksheets(MonthName(i, True))
                    On Error Resume Next
                    For rw = 358 To 3 Step -1
                        'text comparison case-insensitive
                        If UCase(sh.Cells(rw, "A")) = UCase(Ans) Then _
                        sh.Rows(rw).SpecialCells(xlCellTypeConstants).ClearContents
                    Next rw
                    Set sh = Nothing
                    On Error GoTo 0
                Next i
                Exit Do
            Else
                If MsgBox("That entry Is invalid. Do you want To try again?", 36, "Not Found") = vbNo Then Exit Sub
            End If
        End If
    Loop
    
exitsub:
 Application.EnableEvents = True
End Sub

Dave
 
Upvote 0
Solution
Seems to work ok if your sheet(s) have data in them

Try this update

VBA Code:
Sub ClearText()
    Dim wsInput     As Worksheet, sh As Worksheet
    Dim Ans         As Variant
    Dim rw          As Long
    Dim i           As Integer
   
    Set wsInput = Worksheets("Inputs")
   
    Do
        Ans = InputBox("Enter the name of the associate you wish To remove.", "Enter Search Text")
        'cancel pressed
        If StrPtr(Ans) = 0 Then Exit Sub
        If Len(Ans) > 0 Then
            If Application.CountIf(wsInput.Range("A1:A358"), Ans) > 0 Then
                Application.EnableEvents = False
                For i = 1 To 12
                    Set sh = Worksheets(MonthName(i, True))
                    On Error Resume Next
                    For rw = 358 To 3 Step -1
                        'text comparison case-insensitive
                        If UCase(sh.Cells(rw, "A")) = UCase(Ans) Then _
                        sh.Rows(rw).SpecialCells(xlCellTypeConstants).ClearContents
                    Next rw
                    Set sh = Nothing
                    On Error GoTo 0
                Next i
                Exit Do
            Else
                If MsgBox("That entry Is invalid. Do you want To try again?", 36, "Not Found") = vbNo Then Exit Sub
            End If
        End If
    Loop
   
exitsub:
Application.EnableEvents = True
End Sub

Dave
This works!!!!! Very nicely done!! Appreciate your guidance with this project and time to help me out.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,442
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top