If the # of characters exceed 40 in a cell then parts of the value in this cell gets removed

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I need to modify some excel files, some of the values in certain cells in column K exceeds 40
when this happens, I will need to remove these characters from the cell: (SAN), (WET), COVER (any of these cell will only contain one of these words, SAN and WET always has the parenthesis)
example column K values below:
(WET)MH,8'dia,Base,10"w,SOG,144x14"BS,waterstop
(SAN)MH,4'dia,Base,8"w,XYPEX,Drop,Invert,30"
USF AS COVER,Orange County Sanitary, BOCC,FL
1672890966104.png


Code below, this is not working so far... any help is appreciated
VBA Code:
Sub CleanUpStringsLongerThanForty()
    With Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
        If Len("K2:K") > 40 Then
            Replace("COVER", "(SAN)", "(WET)", "")
        End If
    End With
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Replace function works for single cell, not range
Should be:
VBA Code:
Option Explicit
Sub CleanUpStringsLongerThanForty()
Dim cell As Range
For Each cell In Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    If Len(cell) > 40 Then
        cell.Replace "(SAN)", ""
        cell.Replace "(WET)", ""
        cell.Replace "COVER", ""
    End If
Next
End Sub
 
Upvote 0
Solution
Another way that you could try

VBA Code:
Sub CleanUpStringsLongerThanForty_v2()
  With Range("K2", Range("K" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(len(#)>40,substitute(substitute(substitute(#,""(SAN)"",""""),""(WET)"",""""),""COVER"",""""),#)", "#", .Address))
  End With
End Sub
 
Upvote 0
Another way that you could try

VBA Code:
Sub CleanUpStringsLongerThanForty_v2()
  With Range("K2", Range("K" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(len(#)>40,substitute(substitute(substitute(#,""(SAN)"",""""),""(WET)"",""""),""COVER"",""""),#)", "#", .Address))
  End With
End Sub
thank you Peter !
 
Upvote 0
Replace function works for single cell, not range
Should be:
VBA Code:
Option Explicit
Sub CleanUpStringsLongerThanForty()
Dim cell As Range
For Each cell In Range("K2:K" & Range("K" & Rows.Count).End(xlUp).Row)
    If Len(cell) > 40 Then
        cell.Replace "(SAN)", ""
        cell.Replace "(WET)", ""
        cell.Replace "COVER", ""
    End If
Next
End Sub
thank you Bebo ! this works fine
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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