Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Eliminate wild characters

Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.

Function RemAlpha(str As String) As String
With CreateObject("VbScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "[A-Z]"
RemAlpha = .Replace(str, vbNullString)
End With
End Function

I appreciate any help you can give.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Eliminate wild characters

Give this function a try...

Function WildcardsToSpaces(ByVal S As String) As String
Dim X As Long
Const Wildcards As String = "~!@#$%^&*()"
For X = 1 To Len(Wildcards)
S = Replace(S, Mid(Wildcards, X, 1), " ")
Next
WildcardsToSpaces = S
End Function

--
Rick (MVP - Excel)


"MrRJ" wrote in message
...
Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.

Function RemAlpha(str As String) As String
With CreateObject("VbScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "[A-Z]"
RemAlpha = .Replace(str, vbNullString)
End With
End Function

I appreciate any help you can give.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Eliminate wild characters

Function WildStripper(stext) As String
Dim i As Long, j As Long
Dim ba1() As Byte, ba2() As Byte
Const cSTRIP As String = "~!@#$%^&*()"

ba1 = stext
ba2 = cSTRIP
For i = 0 To UBound(ba1) - 1 Step 2
For j = 0 To UBound(ba2)
If ba1(i) = ba2(j) Then
If ba1(i + 1) = ba2(j + 1) Then
ba1(i) = 32
ba1(i + 1) = 0
End If
End If
Next
Next
WildStripper = ba1
End Function

If you have many more characters to remove the above would probably be
faster adapted to use Select case.

This approach might not be suitable with 'wide' characters.

Regards,
Peter T

"MrRJ" wrote in message
...
Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.

Function RemAlpha(str As String) As String
With CreateObject("VbScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "[A-Z]"
RemAlpha = .Replace(str, vbNullString)
End With
End Function

I appreciate any help you can give.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Eliminate wild characters

On second thoughts, go with Rick's Replace for this

Regards,
Peter T

"Peter T" <peter_t@discussions wrote in message
...
Function WildStripper(stext) As String
Dim i As Long, j As Long
Dim ba1() As Byte, ba2() As Byte
Const cSTRIP As String = "~!@#$%^&*()"

ba1 = stext
ba2 = cSTRIP
For i = 0 To UBound(ba1) - 1 Step 2
For j = 0 To UBound(ba2)
If ba1(i) = ba2(j) Then
If ba1(i + 1) = ba2(j + 1) Then
ba1(i) = 32
ba1(i + 1) = 0
End If
End If
Next
Next
WildStripper = ba1
End Function

If you have many more characters to remove the above would probably be
faster adapted to use Select case.

This approach might not be suitable with 'wide' characters.

Regards,
Peter T

"MrRJ" wrote in message
...
Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and
replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.

Function RemAlpha(str As String) As String
With CreateObject("VbScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "[A-Z]"
RemAlpha = .Replace(str, vbNullString)
End With
End Function

I appreciate any help you can give.





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 135
Default Eliminate wild characters

On Apr 15, 4:04*pm, MrRJ wrote:
Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.

Function RemAlpha(str As String) As String
* With CreateObject("VbScript.RegExp")
* * .Global = True
* * .IgnoreCase = True
* * .Pattern = "[A-Z]"
* * RemAlpha = .Replace(str, vbNullString)
* End With
End Function

I appreciate any help you can give.


Mr. RJ,

Excel has a CHAR function (Chr() in VBA). This will return a
corresponding character given a numeric input. CHAR will return
values for the numbers 1 to 255. In other words, the following
wildcards you listed have the corresponding character numbers in
Excel: ~!@#$%^&*(); 126, 33, 64, 35, 36, 37, 94, 38, 42, 40, 41. (As
a side note, if you create a spreadsheet with the CHAR function, then
searching for ~, *, or ? requires a ~ prefix for the find method. For
example, if you want to force Excel to find the ~, then Ctrl + f, Find
What: ~~). Code to do your replacement is below.

[It might be worth it to look into the SUBSTITUTE formula. I've never
had occassion to use SUBSTITUTE nor have I ever used array constants
in a formula before, but I'm sure there is something out there for a
formula that will do the trick for you. Looks like I need to read up
on it myself. I tried the following: =SUBSTITUTE(B17,
{"~","!","@","#","$","%","^","&","*","(",")"}," ") but didn't receive
the desired outcome.]

Best,

Matthew Herbert

Function ReplaceCharacters(Str As String) As String

Dim varArrRep As Variant
Dim strReplaced As String
Dim intI As Integer

'array that contains your desired string replacement
' CHAR function in Excel used to determine values
' CHAR valid for 1 - 255
varArrRep = Array(126, 33, 64, 35, 36, 37, 94, 38, 42, 40, 41)

'store the argument into another variable
strReplaced = Str

'loop through each array item and replace any characters in
' Str that match the varArrRep characters with a space (i.e. Chr(13))
For intI = LBound(varArrRep) To UBound(varArrRep)
strReplaced = Replace(strReplaced, Chr(varArrRep(intI)), Chr(13))
Next

'return the result to the function
ReplaceCharacters = strReplaced

End Function


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Eliminate wild characters

On Wed, 15 Apr 2009 13:04:01 -0700, MrRJ
wrote:

Hi,
Is there a VBA code to eliminate wild characters ~!@#$%^&*() and replacing
them with a space?
I found this on this site and looks like it could work but need to
incoporate the wild characters and in a specific column.

Function RemAlpha(str As String) As String
With CreateObject("VbScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "[A-Z]"
RemAlpha = .Replace(str, vbNullString)
End With
End Function

I appreciate any help you can give.


Something like this:

==================================
Option Explicit
Sub KillWild()
Dim rng As Range, c As Range
Dim re As Object

Set rng = Selection 'or whatever
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "[~!@#$%^&*]"

For Each c In rng
c.Value = re.Replace(c.Value, " ")
Next c
End Sub
==================================

will remove the characters in your list and replace each one with a <space.

In the above, rng is set to "Selection". But you could just as easily set it
to a specified range.

Also, as written, the function will replace *each* wild character with a space;
so if you have several in a row, there will be several spaces; or if there is a
space followed by a wild character, there will be several spaces.

If you want to only be left with a single space in those instances, make this
small change:


re.Pattern = "[\s~!@#$%^&*]+"
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wild characters in function? Steven Excel Worksheet Functions 3 November 4th 09 11:24 PM
Eliminate wild characters MrRJ Excel Discussion (Misc queries) 5 April 15th 09 10:21 PM
Countif using Wild Card Characters nebb Excel Worksheet Functions 2 February 11th 06 12:14 AM
Using wild characters for an array Steven Excel Discussion (Misc queries) 2 February 9th 05 04:29 PM
Using wild card characters in array formulas PJB Shark Excel Worksheet Functions 3 January 19th 05 03:09 PM


All times are GMT +1. The time now is 09:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"