Trim non-alphanumeric characters… with the exception of semi-colons

ellison

Active Member
Joined
Aug 1, 2012
Messages
356
Office Version
  1. 365
Platform
  1. Windows
Hi, we have a brilliantly helpful UDF which removes any non-alphanumeric (0-9 or a-z or A-Z) characters out of any cell.

We were wondering if it would be possible to tweak this in order to leave alone any semi-colons? i.e. not trim them out / replace them

God knows how many non alpha numeric characters there are, but I’ve tried to include a few of them below in an example here…

Also, I should point out:

  • the info in the Original-String is sometimes blank
  • there can be multiple semi-colons in the info in the Original-String (or none)
Original-StringCleanCodeV2CleanCode-Original
;ABC-12-434;XYX,002;ABC12434;XYX002ABC12434XYX002
3879-001;43/669;3879001;43669;387900143669
61a4_2B C;test61A42BC;TEST61A42BCTEST




Also, we wouldn’t need to see the info in Clean-Code-Original, it’s just there to help throw some light on my ramblings 😊. It’s what this (brilliant!) UDF does:

UDF: Function CleanCode(Rng As Range)

Dim strTemp As String

Dim n As Long



For n = 1 To Len(Rng)

Select Case Asc(Mid(UCase(Rng), n, 1))

Case 48 To 57, 65 To 90

strTemp = strTemp & Mid(UCase(Rng), n, 1)

End Select

Next

CleanCode = strTemp

End Function
 

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:
Case 48 To 57, 65 To 90, 59
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,044
Members
452,542
Latest member
Bricklin

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