Find and Replace

whitoulias

Board Regular
Joined
Jun 22, 2012
Messages
153
Hi all,

I have created a macro which reads column B and then creates separate sheets based on the values-text found on that Column.

I obviously get an error while new sheets are created. The 1st one is when text is over 31 characters. This is resolved.

The 2nd one is when i have the 7 sin characters (/, \, *, [, ], ? , :, ) and sheet name cannot be created.

Ideally i would like to have the below code like 7 times, each for every character, but this doesn't work.

Range("B2:B" & lastRow(ActiveSheet)).Select
Selection.Replace What:="/", Replacement:="_", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False

Any thought would be appreciated.

Thank you
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try...

Code:
    Dim sinChars As Variant
    sinChars = Array("/", "\", "~*", "[", "]", "~?", ":")
    
    Dim i As Long
    With Range("B2:B" & lastRow(ActiveSheet))
        For i = LBound(sinChars) To UBound(sinChars)
            .Replace what:=sinChars(i), replacement:="_", Lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
        Next i
    End With

Note that since the asterisk (*) and question mark (?) are wildcards, the escape character tilde (~) is used in order to refer to the actual character.

Hope this helps!
 
Last edited:
Upvote 0
You were right about the asterisk (*) and question mark (?). That is why my code did not work in the 1st place. I should have thought.
Your code works great.
Thank you for your time!!!
 
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

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