Question

Goal: I am looking for a macro that can delete multiple rows based on cell criteria in one column, but I want the macro to ask for a value each time it is run rather than having a set value included in code. Each code that I have found online so far either does not work or is coded for only one value.

I am using excel 2003

Here is the one code that I've found that works for my purpose.. but I would like to edit it somehow so that it prompts the user to input a certain number, rather than use the same number over and over again.

      Sub Delete_Rows()
            Dim rng As Range, cell As Range, del As Range
            Set rng = Intersect(Range("A2:J707"), ActiveSheet.UsedRange)
            For Each cell In rng
            If (cell.Value) = "201" _
            Then
            If del Is Nothing Then
            Set del = cell
            Else: Set del = Union(del, cell)
            End If
            End If
            Next cell
            On Error Resume Next
            del.EntireRow.Delete
        End Sub
Was it helpful?

Solution

You should check the InputBox function

Basically, it displays a prompt in a dialog box, waits for the user to input text or click a button, and then returns a string containing the contents of the text box.

So, for your code, it would be like this :

 Sub Delete_Rows()
    Dim selectedValue As Integer
    selectedValue = InputBox ("Please, enter a number", "Input for deleting row", Type:=1)
                              'Prompt                   'Title                  'Value type (number here)
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("A2:J707"), ActiveSheet.UsedRange)
    For Each cell In rng
    If (cell.Value) = selectedValue _
    Then
    If del Is Nothing Then
    Set del = cell
    Else: Set del = Union(del, cell)
    End If
    End If
    Next cell
    On Error Resume Next
    del.EntireRow.Delete
End Sub

OTHER TIPS

Try this. It works by first selecting the range desired, then running the macro. Really only the first and last row are important in the range, so the range can be just one column in width. It will delete all rows within the range selected whose values in the column entered match the value entered.

Sub DeleteRows()
    Application.ScreenUpdating = False

    Dim msg As String, title As String
    Dim col As Integer
    Dim value As String

    msg = "Enter column number:"
    title = "Choose column"
    col = InputBox(msg, title)

    msg = "Enter string to search for:"
    title = "Choose search string"
    value = InputBox(msg, title)

    Dim rSt As Integer, rEn As Integer
    rSt = Selection.Rows(1).Row
    rEn = rSt + Selection.Rows.Count - 1

    Dim r As Integer
    r = rSt
    While r <= rEn
        If Cells(r, col).value = value Then
            Rows(r).EntireRow.Delete Shift:=xlUp
            rEn = rEn - 1
        Else
            r = r + 1
        End If
    Wend

    Application.ScreenUpdating = True
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top