Clear contents of cell

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
330
Office Version
  1. 365
Platform
  1. Windows
Is there a VBA code that I can use to clear contents of a cell if it ONLY contains a dash (-)? Some of the cells have a dash (-) with other characters & numbers. I want to keep those. I would like to run this for columns A-AP

EXAMPLE
CURRENT
WANTED RESULTS
BANANAS - APPLES - ORANGES
BANANAS - APPLES - ORANGES
-
CARROTS - CUCUMBERS
CARROTS - CUCUMBERS
-
12-09-2016
12-09-2019

<tbody>
</tbody>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

frabulator

Board Regular
Joined
Jun 27, 2014
Messages
181
Office Version
  1. 2019
Platform
  1. Windows
The simple answer would be

Code:
If Selection.Value = "-" Then
    Selection.Value = ""
End If

---Edit----
if you want to remove ever '-' in the workbook you could do this:

Code:
[COLOR=#333333][FONT=Verdana]Dim sht As Worksheet[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]Dim x As Long[/FONT][/COLOR]





[COLOR=#333333][FONT=Verdana]'Loop through each worksheet in ActiveWorkbook[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]For Each sht In ActiveWorkbook.Worksheets[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]sht.Cells.Replace What:="-", Replacement:="", _[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]SearchFormat:=False, ReplaceFormat:=False[/FONT][/COLOR]
[COLOR=#333333][FONT=Verdana]Next sht[/FONT][/COLOR]
 
Last edited:
Upvote 0

frabulator

Board Regular
Joined
Jun 27, 2014
Messages
181
Office Version
  1. 2019
Platform
  1. Windows
Thank you.

Do I need to enter a range or anything else?

That code is based on the current selected cell, one cell. If you would like to have a selection of ranges then use the code below:

Code:
Sub del()


    Dim r As Long
    Dim c As Long
    Dim rc As Long
    Dim cc As Long
    
    r = Selection.Row
    c = Selection.Column
    rc = Selection.Rows.Count
    cc = Selection.Columns.Count
    
    For ro = r To rc + r - 1
        For co = c To cc + c - 1
            If ThisWorkbook.ActiveSheet.Cells(ro, co).Value = "-" Then
                ThisWorkbook.ActiveSheet.Cells(ro, co).Value = ""
            End If
        Next
    Next
    
    


End Sub

---EDIT---
The above code works by selection. Select a range and then run the macro. Inside the selection if a cells value is "-" then it will replace it with a "" (ie. an empty value).
 
Last edited:
Upvote 0

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
330
Office Version
  1. 365
Platform
  1. Windows
thank you for the help.
 
Upvote 0

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
330
Office Version
  1. 365
Platform
  1. Windows
That code is based on the current selected cell, one cell. If you would like to have a selection of ranges then use the code below:

Code:
Sub del()


    Dim r As Long
    Dim c As Long
    Dim rc As Long
    Dim cc As Long
    
    r = Selection.Row
    c = Selection.Column
    rc = Selection.Rows.Count
    cc = Selection.Columns.Count
    
    For ro = r To rc + r - 1
        For co = c To cc + c - 1
            If ThisWorkbook.ActiveSheet.Cells(ro, co).Value = "-" Then
                ThisWorkbook.ActiveSheet.Cells(ro, co).Value = ""
            End If
        Next
    Next
    
    


End Sub

---EDIT---
The above code works by selection. Select a range and then run the macro. Inside the selection if a cells value is "-" then it will replace it with a "" (ie. an empty value).


I am getting missmatch error when using both versions of the code.
 
Upvote 0

frabulator

Board Regular
Joined
Jun 27, 2014
Messages
181
Office Version
  1. 2019
Platform
  1. Windows
I am getting missmatch error when using both versions of the code.

Everything is working as expected on my end. Try to change the names of the variables (ie. change 'r' to 'SelR') and see if that fixes it. It might be that there are pre defined variables that share the same name as the ones used in that sub.
 
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
67,109
Office Version
  1. 365
Platform
  1. Windows
Loops are totally unnecessary and inefficient in this case. The Find/Replace functionality has the option to match on the entire cell contents. This means it will only replace if the ENTIRE cell equals what you are looking for.

You could either use Find/Replace and do this all at once (no VBA required), or turn on the Macro Recorder to get the VBA code needed for it, i.e.
Code:
    Columns("A:AP").Replace What:="-", Replacement:="", [COLOR=#ff0000]LookAt:=xlWhole[/COLOR], _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Last edited:
Upvote 0

frabulator

Board Regular
Joined
Jun 27, 2014
Messages
181
Office Version
  1. 2019
Platform
  1. Windows
Loops are totally unnecessary and inefficient in this case. The Find/Replace functionality has the option to match on the entire cell contents. This means it will only replace if the ENTIRE cell equals what you are looking for.

You could either use Find/Replace and do this all at once (no VBA required), or turn on the Macro Recorder to get the VBA code needed for it, i.e.
Code:
    Columns("A:AP").Replace What:="-", Replacement:="", [COLOR=#ff0000]LookAt:=xlWhole[/COLOR], _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


Joe is correct. That is a much better method then what I was doing :)
 
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
67,109
Office Version
  1. 365
Platform
  1. Windows
A lot of people don't realize that the "Match entire cell contents" (which is the "LookAt" argument in VBA) exists.
You don't see it from the Find/Replace menu unless you click on the Options button.
It is a very useful little option!:)
 
Upvote 0

Forum statistics

Threads
1,191,578
Messages
5,987,403
Members
440,096
Latest member
yanaungmyint

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