In a defined range, clear contents of all cells containg only one character

Zembu

New Member
Joined
Apr 9, 2016
Messages
23
Tazguy37 gave a solution to a slightly different question!

He wrote;

Sub deleteSingles()
Dim cell As Range

On Error Resume Next
For Each cell In Cells.SpecialCells(xlCellTypeConstants)
If Len(cell.Value) = 1 Then cell.Delete
Next cell

End Sub


This solution is almost good for me! I want to (not delete but clear contents) of cells containg a single character, and be able to set a specific range to be operated on.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
just change cell.delete to cell.ClearContents
 
Upvote 0
VBA Code:
Sub deleteSingles()
Dim cell As Range

On Error Resume Next
For Each cell In Cells.SpecialCells(xlCellTypeConstants)
If Len(cell.Value) = 1 Then cell.ClearContents
Next cell

End Sub
 
Upvote 0
Hi Zembu,

instead of referring to all constants in the sheet maybe use a constant for the address of the range like
VBA Code:
Sub ClearContentsSingles()
Dim rngCell As Range

Const cstrRange As String = "A2:E50"

For Each rngCell In Range(cstrRange)
  If Len(rngCell.Value) = 1 Then rngCell.ClearContents
Next rngCell

End Sub
Ciao,
Holger
 
Upvote 0
@HaHoBe that will also clear cells with formulae that return a single character. ;)
 
Upvote 0
VBA Code:
Sub deleteSingles()
Dim cell As Range

On Error Resume Next
For Each cell In Cells.SpecialCells(xlCellTypeConstants)
If Len(cell.Value) = 1 Then cell.ClearContents
Next cell

End Sub
Thanks Keef - I needed to define the range as well HaHoBe seems to have it as I need.
 
Upvote 0
Hi Zembu,

instead of referring to all constants in the sheet maybe use a constant for the address of the range like
VBA Code:
Sub ClearContentsSingles()
Dim rngCell As Range

Const cstrRange As String = "A2:E50"

For Each rngCell In Range(cstrRange)
  If Len(rngCell.Value) = 1 Then rngCell.ClearContents
Next rngCell

End Sub
Ciao,
Holger
Thanks HaHoBe - I am running yours at the moment - it is taking a long time - my selection of cells is 1,365,910. I could see that it cleared the contents of some so I guess it will get there eventually. Many thanks
 
Upvote 0
Hi Zembu,

please mind what Fluff has put in: it will kill formulas.

If you care about the time the macro takes maybe try
VBA Code:
Sub ClearContentsSingles_2()
Dim rngCell As Range

Const cstrRange As String = "A2:E50"

With Application
  .ScreenUpdating = False
  .EnableEvents = False
  .Calculation = xlCalculationManual
End With

On Error Resume Next
For Each rngCell In Range(cstrRange).SpecialCells(xlCellTypeConstants)
  If Len(rngCell.Value) = 1 Then rngCell.ClearContents
Next rngCell

With Application
  .ScreenUpdating = True
  .EnableEvents = True
  .Calculation = xlCalculationAutomatic
End With

End Sub
Ciao,
Holger
 
Upvote 0
Solution
Hi Zembu,

please mind what Fluff has put in: it will kill formulas.

If you care about the time the macro takes maybe try
VBA Code:
Sub ClearContentsSingles_2()
Dim rngCell As Range

Const cstrRange As String = "A2:E50"

With Application
  .ScreenUpdating = False
  .EnableEvents = False
  .Calculation = xlCalculationManual
End With

On Error Resume Next
For Each rngCell In Range(cstrRange).SpecialCells(xlCellTypeConstants)
  If Len(rngCell.Value) = 1 Then rngCell.ClearContents
Next rngCell

With Application
  .ScreenUpdating = True
  .EnableEvents = True
  .Calculation = xlCalculationAutomatic
End With

End Sub
Ciao,
Holger
Many thanks HaHoBe. My range does not include formulas so that's OK. I will let it run for a while more, but will also copy your latest and try that another time.
Cheers,

Andrew
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

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