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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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,213,543
Messages
6,114,245
Members
448,555
Latest member
RobertJones1986

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