VBA - Clear cell contents in range if less than 5 characters

The Great SrH

Board Regular
Joined
Jan 16, 2015
Messages
179
Hi,


I'm hoping somebody can help with a code to clear the contents of a cell in a range, if that cell contains less than 5 characters (it may be a "." or "yes", etc.)

Starting in row 2, Columns E to K will contain an extract of comments. However, some cells contain a full stop, "N/A" etc which we don't require.


I would like a macro which will only search those columns, and clear the contents if less than 5 characters.


Thanks in advance
 
Amend like this and it runs
Code:
Dim arr As Variant
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I think changing it to Variant should work.

Code:
Dim arr()   As Variant
 
Upvote 0
Thanks, yes it should be As Variant, I've also added test if the last row is less than 2:
Rich (BB code):
Sub ClearCells()


    Dim x       As Long
    Dim y       As Long
    Dim arr()   As Variant
    
    x = Cells(Rows.Count, 11).End(xlUp).Row
    
    If x < 2 Then End
    
    arr = Cells(2, 5).Resize(x - 1, 7).Value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        For y = LBound(arr, 2) To UBound(arr, 2)
            If Len(arr(x, y)) < 5 Then arr(x, y) = vbNullString
        Next y
    Next x
    
    Cells(2, 5).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    
    Erase arr
    
End Sub
 
Upvote 0
Thanks, yes it should be As Variant, I've also added test if the last row is less than 2:
Rich (BB code):
Sub ClearCells()


    Dim x       As Long
    Dim y       As Long
    Dim arr()   As Variant
    
    x = Cells(Rows.Count, 11).End(xlUp).Row
    
    If x < 2 Then End
    
    arr = Cells(2, 5).Resize(x - 1, 7).Value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        For y = LBound(arr, 2) To UBound(arr, 2)
            If Len(arr(x, y)) < 5 Then arr(x, y) = vbNullString
        Next y
    Next x
    
    Cells(2, 5).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    
    Erase arr
    
End Sub
This works perfectly, and very quickly.

Thanks so much everyone for your help!
 
Upvote 0
Thanks, yes it should be As Variant, I've also added test if the last row is less than 2:
Rich (BB code):
Sub ClearCells()


    Dim x       As Long
    Dim y       As Long
    Dim arr()   As Variant
    
    x = Cells(Rows.Count, 11).End(xlUp).Row
    
    If x < 2 Then End
    
    arr = Cells(2, 5).Resize(x - 1, 7).Value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        For y = LBound(arr, 2) To UBound(arr, 2)
            If Len(arr(x, y)) < 5 Then arr(x, y) = vbNullString
        Next y
    Next x
    
    Cells(2, 5).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    
    Erase arr
    
End Sub

Sorry - I thought i was sorted.


If i wanted to extend to column M (13), i assumed the simple change would be:


Code:
x = Cells(Rows.Count, 13).End(xlUp).Row



However, this doesn't seem to work?



Thanks
 
Upvote 0
It's starting in column E so it's this line to change to extend to M:
Rich (BB code):
arr = Cells(2, 5).Resize(x - 1, 9).Value
Where column E is column index 5 and column M's index, relative to E is 9.

For count of cells between a start row and end row or start column and end column is always: End position - Start position + 1 so here, 13(M) - 5(E) + 1 = 9 (count of columns between E and M inclusive)
 
Last edited:
Upvote 0
It's starting in column E so it's this line to change to extend to M:
Rich (BB code):
arr = Cells(2, 5).Resize(x - 1, 9).Value
Where column E is column index 5 and column M's index, relative to E is 9.

For count of cells between a start row and end row or start column and end column is always: End position - Start position + 1 so here, 13(M) - 5(E) + 1 = 9 (count of columns between E and M inclusive)

Thanks so much for this and the detailed explanation!
 
Upvote 0
You're welcome, hopefully next time you'll edit the correct line!
 
Upvote 0

Forum statistics

Threads
1,224,940
Messages
6,181,887
Members
453,068
Latest member
DCD1872

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