VBA code adding values to cells instead of removing

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I have this macro that removed certain parts of the text if the number of characters in any cell in column K is longer than 40.
However, it is also adding a 0 to any cell in K when the number of characters is 0. See picture below
code also below. Any help is greatly appreciated

1673900923069.png


VBA Code:
Sub CleanUpStringsLongerThanForty()
   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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
VBA Code:
Sub CleanUpStringsLongerThanForty()
   With Range("K2", Range("K" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",if(len(#)>40,substitute(substitute(substitute(#,""(San)"",""""),""(WET)"",""""),""COVER"",""""),#))", "#", .Address))
  End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub CleanUpStringsLongerThanForty()
   With Range("K2", Range("K" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(#="""","""",if(len(#)>40,substitute(substitute(substitute(#,""(San)"",""""),""(WET)"",""""),""COVER"",""""),#))", "#", .Address))
  End With
End Sub
Fluff, thank you. This works great
so the
VBA Code:
#="""","""",
means the text string in column K has to contain at least 2 text strings with comma between the two for this code to execute ?
 
Upvote 0
No it means if the cell is empty leave it that way.
 
Upvote 0

Forum statistics

Threads
1,215,269
Messages
6,123,976
Members
449,138
Latest member
abdahsankhan

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