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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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