VB formula to clear cell content exceeding dcharacters

ATY807

New Member
Joined
Mar 18, 2020
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I need a VB code that clears contents of cells of a selected column in an Excel sheet when the number of characters exceeds 12

Thank you,
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this code - the range is set to column A but you can amend that as needed.

VBA Code:
Sub ClearCells()
    Dim cel As Range
    For Each cel In Range("A:A")
        If Len(cel.Text) > 12 Then cel.ClearContents
    Next cel
End Sub
 
Upvote 0
Here is another macro that you can try...
VBA Code:
Sub ClearIfMoreThan12Characters()
  Columns("A").Replace "?????????????*", "", xlPart, , , , False, False
End Sub

EDIT NOTE: You can replace Columns("A") with Selection since that is what you apparently asked for originally.
 
Upvote 0
Thanks Rick. How do I modify it to keep only 12 characters,
Try this code - the range is set to column A but you can amend that as needed.

VBA Code:
Sub ClearCells()
    Dim cel As Range
    For Each cel In Range("A:A")
        If Len(cel.Text) > 12 Then cel.ClearContents
    Next cel
End Sub


Thank you for your reply.

Missed an End if statement, otherwise works. A little slow due to looping over entire column A:A selection, need to specify the exact range in which there are cell contents.
 
Upvote 0
Here is another macro that you can try...
VBA Code:
Sub ClearIfMoreThan12Characters()
  Columns("A").Replace "?????????????*", "", xlPart, , , , False, False
End Sub

EDIT NOTE: You can replace Columns("A") with Selection since that is what you apparently asked for originally.
Thank you
 
Upvote 0
How about
VBA Code:
Sub aty()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if(len(" & .Address & ")<>12,""""," & .Address & ")")
   End With
End Sub
 
Upvote 0
Or if you want it to work on a selected column, use
VBA Code:
Sub aty()
   With Intersect(ActiveSheet.UsedRange, Selection.EntireColumn)
      .Value = Evaluate("if(len(" & .Address & ")<>12,""""," & .Address & ")")
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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