VBA to delete all rows NOT equal to value in inputbox

Dustinkli

New Member
Joined
Mar 26, 2019
Messages
28
Hello,


I would like to be able to delete all rows in a spreadsheet based on a specified value in a selected column. I currently have a macro to delete a row based on specified cells but I'm not sure how to make it delete all rows with cells in a column EXCEPT a specified cell based on an input box. The one I have is based on .autofilter but I don't know if it has the capability to do what I need.

Basically I'd like to put the cell to preserve and it deletes all rows where that cell isn't in that column.

Can anyone point me in the right direction?
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
45,283
Office Version
365
Platform
Windows
What about you post the code you have and it would probably just require a slight modification to do the new job.

About how many rows of data altogether are you likely yo have in your sheet?
 

Dustinkli

New Member
Joined
Mar 26, 2019
Messages
28
I think I found a solution:

Code:
Sub DeleteRows()
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
xTitleId = "DeleteRows"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Select Column Range :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("Text to Preserve", xTitleId, Type:=2)
For Each rng In InputRng
    If rng.Value <> DeleteStr Then
        If DeleteRng Is Nothing Then
            Set DeleteRng = rng
        Else
            Set DeleteRng = Application.Union(DeleteRng, rng)
        End If
    End If
Next
DeleteRng.EntireRow.Delete
 End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,102,840
Messages
5,489,191
Members
407,675
Latest member
meaghutter

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top