使用Excel VBA删除所有字母

使用Excel VBA删除所有字母,excel,vba,Excel,Vba,所以我有这个代码,想添加一些代码,删除字母表中的所有字母。工作范围为E2:E200 例如,33 CONTIN,BOOST,65NA,它们将显示33,(空白),65 我试过网上的几种代码,但似乎都不管用 任何帮助都将不胜感激 谢谢,正则表达式。这是VBScript(在VBA中工作),但使用wsh对象。您需要使用Excel或其他任何工具来托管它。ObjArgsare命令行参数-文件名、搜索字符串、替换字符串 Elseif objArgs.count=3然后 设置srcfile=fso.GetFile

所以我有这个代码,想添加一些代码,删除字母表中的所有字母。工作范围为E2:E200

例如,
33 CONTIN,BOOST,65NA
,它们将显示
33,(空白),65

我试过网上的几种代码,但似乎都不管用

任何帮助都将不胜感激


谢谢,正则表达式。这是VBScript(在VBA中工作),但使用wsh对象。您需要使用Excel或其他任何工具来托管它。ObjArgsare命令行参数-文件名、搜索字符串、替换字符串

Elseif objArgs.count=3然后
设置srcfile=fso.GetFile(objArgs(0))
报告错误“srcFile”
如果err.number=0,则设置TS=srcFile.OpenAsTextStream(1,0)
如果错误号为0,则
wscript.echo err.description&&srcFile.path,48,“Serenity的搜索”
清楚
其他的
报告错误“TS”和“&srcFile.path”
Src=ts.readall
如果err.number=62,则
清楚
其他的
ReportErrors“reads”&“&srcFile.path
regEx.Pattern=objArgs(1)
regEx.IgnoreCase=True
regEx.Global=True
NewSrc=regEx.Replace(Src,objArgs(2))
如果是NewSrcSrc,那么
wscript.echo“替换在”&srcfile.path,64,“Serenity的搜索”
关闭
设置TS=srcFile.OpenAsTextStream(2,0)
写新闻
报告错误“写入文件”
如果结束
如果结束
如果结束
报告错误“检查确定”和“&srcFile.path”
其他的
在正则表达式的编写模式中使用特殊字符和序列。下表描述并给出了可使用的字符和序列的示例。 字符描述 \ 将下一个字符标记为特殊字符或文字。例如,“n”与字符“n”匹配。“\n”与换行符匹配。序列“\\”匹配“\”和“\(“匹配”()。 ^ 匹配输入的开头。 $ 匹配输入的结尾。 * 与前面的字符零次或多次匹配。例如,“zo*”与“z”或“zoo”匹配。 + 匹配前面的字符一次或多次。例如,“zo+”匹配“zoo”,但不匹配“z”。 ? 与前面的字符零次或一次匹配。例如,“a?ve?”与“从不”中的“ve”匹配。 . 匹配除换行符以外的任何单个字符。 (模式) 匹配模式并记住匹配项。可以使用项[0]…[n]从生成的Matches集合中检索匹配的子字符串。若要匹配括号字符(),请使用“\(”或“\”)。 x | y 匹配x或y。例如,“z|wood”匹配“z”或“wood”。(z|w)oo“匹配“zoo”或“wood”。 {n} n是非负整数。正好匹配n次。例如,“o{2}”与“Bob”中的“o”不匹配,但与“fooood”中的前两个o匹配。 {n,} n是非负整数。至少匹配n次。例如,“o{2,}”与“Bob”中的“o”不匹配,而与“fooood”中的所有o匹配。“o{1,}”相当于“o+”。“o{0,}”相当于“o*”。 {n,m} m和n是非负整数。至少匹配n次,最多匹配m次。例如,“o{1,3}”匹配“foooood”中的前三个o。“o{0,1}”相当于“o?”。 [xyz] 字符集。匹配任何一个封闭字符。例如,“[abc]”匹配“普通”中的“A”。 [^xyz] 负数字符集。匹配任何未包含在内的字符。例如,“[^abc]”匹配“普通”中的“p”。 [a-z] 字符范围。匹配指定范围内的任何字符。例如,“[A-z]”匹配“A”到“z”范围内的任何小写字母字符。 [^m-z] 负范围字符。匹配不在指定范围内的任何字符。例如,“[m-z]”匹配不在“m”到“z”范围内的任何字符。 \b 匹配单词边界,即单词和空格之间的位置。例如,“er\b”匹配“从不”中的“er”,但不匹配“动词”中的“er”。 \B 匹配非单词边界。“ea*r\B”匹配“从不早”中的“ear”。 \d 匹配一个数字字符。相当于[0-9]。 \D 匹配非数字字符。相当于[^0-9]。 \f 匹配表单提要字符。 \n 匹配换行符。 \r 匹配回车符。 \ 匹配任何与“[\f\n\r\t\v]”相等的空白,包括空格、制表符、表单提要等。 \ 匹配任何非空白字符。相当于“[^\f\n\r\t\v]”。 \t 匹配制表符。 \五 匹配垂直制表符。 \w 匹配包括下划线在内的任何单词字符。相当于“[A-Za-z0-9_]”。 \W 匹配任何非单词字符。相当于“[^A-Za-z0-9_33;]”。 \num 匹配num,其中num是一个正整数。返回到记住的匹配的引用。例如,“()\1”匹配两个连续的相同字符。 \n 匹配n,其中n是八进制转义值。八进制转义值必须为1、2或3位。例如,“\11”和“\011”都匹配制表符。“\0011”相当于“\001”和“1”.八进制转义值不得超过256。如果超过256,则只有前两位数字构成表达式。允许在正则表达式中使用ASCII码。 \xn 匹配n,其中n是十六进制转义值。十六进制转义值的长度必须正好是两位数。例如,“\x41”匹配“a”。“\x041”相当于“\x04”和“1”。允许在正则表达式中使用ASCII代码。 评论 下面的代码演示了Pattern属性的使用。 复制代码 函数RegExpTest(patrn、strng) Dim regEx,Match,Matches'创建变量。 Set regEx=New RegExp'创建正则表达式。 regEx.Pattern=patrn'集
Elseif objArgs.count = 3 then
        Set srcfile = fso.GetFile(objArgs(0))
        ReportErrors "srcFile"
        If err.number = 0 then Set TS = srcFile.OpenAsTextStream(1, 0)
        If err.number <> 0 then
            wscript.echo err.description & " " & srcFile.path, 48, "Serenity's Search" 
            err.clear
        else
            ReportErrors "TS" & "     " & srcFile.path
            Src=ts.readall
            If err.number = 62 then
                err.clear
            else
                ReportErrors "ReadTS" & "     " & srcFile.path
                regEx.Pattern = objArgs(1) 
                regEx.IgnoreCase = True
                regEx.Global = True
                NewSrc= regEx.Replace(Src, objArgs(2)) 
                If NewSrc<>Src then
                    wscript.echo "Replacement made in " & srcfile.path, 64, "Serenity's Search" 
                    TS.close
                    Set TS = srcFile.OpenAsTextStream(2, 0)
                    ts.write newsrc
                    ReportErrors "Writing file"
                End If
            End If
        End If
        ReportErrors "Check OK" & "     " & srcFile.path


Else
Special characters and sequences are used in writing patterns for regular expressions. The following table describes and gives an example of the characters and sequences that can be used. Character Description \ Marks the next character as either a special character or a literal. For example, "n" matches the character "n". "\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(". ^ Matches the beginning of input. $ Matches the end of input. * Matches the preceding character zero or more times. For example, "zo*" matches either "z" or "zoo". + Matches the preceding character one or more times. For example, "zo+" matches "zoo" but not "z". ? Matches the preceding character zero or one time. For example, "a?ve?" matches the "ve" in "never". . Matches any single character except a newline character. (pattern) Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]...[n]. To match parentheses characters ( ), use "\(" or "\)". x|y Matches either x or y. For example, "z|wood" matches "z" or "wood". "(z|w)oo" matches "zoo" or "wood". {n} n is a nonnegative integer. Matches exactly n times. For example, "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood". {n,} n is a nonnegative integer. Matches at least n times. For example, "o{2,}" does not match the "o" in "Bob" and matches all the o's in "foooood." "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*". { n , m } m and n are nonnegative integers. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?". [ xyz ] A character set. Matches any one of the enclosed characters. For example, "[abc]" matches the "a" in "plain". [^ xyz ] A negative character set. Matches any character not enclosed. For example, "[^abc]" matches the "p" in "plain". [ a-z ] A range of characters. Matches any character in the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z". [^ m-z ] A negative range characters. Matches any character not in the specified range. For example, "[m-z]" matches any character not in the range "m" through "z". \b Matches a word boundary, that is, the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb". \B Matches a non-word boundary. "ea*r\B" matches the "ear" in "never early". \d Matches a digit character. Equivalent to [0-9]. \D Matches a non-digit character. Equivalent to [^0-9]. \f Matches a form-feed character. \n Matches a newline character. \r Matches a carriage return character. \s Matches any white space including space, tab, form-feed, etc. Equivalent to "[ \f\n\r\t\v]". \S Matches any nonwhite space character. Equivalent to "[^ \f\n\r\t\v]". \t Matches a tab character. \v Matches a vertical tab character. \w Matches any word character including underscore. Equivalent to "[A-Za-z0-9_]". \W Matches any non-word character. Equivalent to "[^A-Za-z0-9_]". \num Matches num, where num is a positive integer. A reference back to remembered matches. For example, "(.)\1" matches two consecutive identical characters. \ n Matches n, where n is an octal escape value. Octal escape values must be 1, 2, or 3 digits long. For example, "\11" and "\011" both match a tab character. "\0011" is the equivalent of "\001" & "1". Octal escape values must not exceed 256. If they do, only the first two digits comprise the expression. Allows ASCII codes to be used in regular expressions. \xn Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04" & "1". Allows ASCII codes to be used in regular expressions. Remarks The following code illustrates the use of the Pattern property. Copy Code Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' Create variable. Set regEx = New RegExp ' Create a regular expression. regEx.Pattern = patrn ' Set pattern. regEx.IgnoreCase = True ' Set case insensitivity. regEx.Global = True ' Set global applicability. Set Matches = regEx.Execute(strng) ' Execute search. For Each Match in Matches ' Iterate Matches collection. RetStr = RetStr & "Match found at position " RetStr = RetStr & Match.FirstIndex & ". Match Value is '" RetStr = RetStr & Match.Value & "'." & vbCRLF Next RegExpTest = RetStr End Function MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))