Deleting cell contents which contain negative values

e_sauven

New Member
Joined
Apr 3, 2014
Messages
27
Hi

I'm trying to clear the content of cells in a large table that contain a negative value using some VBA.

Any help greatly appreciated.

Thanks

Emilie
 

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

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,701
Office Version
  1. 365
Platform
  1. Windows
You'll need to change the red parts in the code to fit your requirements but try:
Rich (BB code):
Sub Macro1()

Dim i As Long

With ActiveSheet
    .AutoFilterMode = False
    i = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("A1:J" & i).AutoFilter
    .Range("A1:J" & i).AutoFilter Field:=5, Criteria1:="<0"
    .Range("A1:J" & i).Offset(1).Resize(i - 1).SpecialCells(xlCellTypeVisible).ClearContents
    .AutoFilterMode = False
End With

End Sub
 
Last edited:

VBA Geek

MrExcel MVP
Joined
Dec 16, 2013
Messages
2,857
Code:
Sub RemCellWithNegVal()
    Dim iRange As Range
    On Error Resume Next
    Set iRange = Application.InputBox("Select the Range", , , , , , , 8)
    If iRange Is Nothing Then Exit Sub
    On Error GoTo 0
     iRange = Evaluate("IF(" & iRange.Address & "<0, """", " & iRange.Address & ")")
End Sub
 

VBA Geek

MrExcel MVP
Joined
Dec 16, 2013
Messages
2,857
to be complete,

here's a version that wont return annoying 0's if blank cells are found within your selected range

Code:
Sub RemCellWithNegVal()
    Dim iRange As Range
    On Error Resume Next
    Set iRange = Application.InputBox("Select the Range", , , , , , , 8)
    If iRange Is Nothing Then Exit Sub
    On Error GoTo 0
    iRange = Evaluate("IF(" & iRange.Address & "="""","""",IF(" & iRange.Address & "<0,"""",A1:E18))")
end sub
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Possibly this?
Code:
Sub DeleteNegatives()
  Range("A2:J11").Replace What:="-*", Replacement:="", LookAt:=xlWhole, SearchFormat:=False, ReplaceFormat:=False
End Sub

Which raises the issue of whether you really need a macro or could possibly just use the Find/Replace method manually?
 

e_sauven

New Member
Joined
Apr 3, 2014
Messages
27
Possibly this?
Code:
Sub DeleteNegatives()
  Range("A2:J11").Replace What:="-*", Replacement:="", LookAt:=xlWhole, SearchFormat:=False, ReplaceFormat:=False
End Sub

Which raises the issue of whether you really need a macro or could possibly just use the Find/Replace method manually?

Thanks that's fantastic, just what I needed

Really appreciate it, thanks all

Emilie
 

Watch MrExcel Video

Forum statistics

Threads
1,123,135
Messages
5,599,917
Members
414,348
Latest member
KloppyM

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
Top