Excel VBA:搜索字符串以找到第一个非文本字符 [英] Excel VBA: search a string to find the first non-text character

查看:637
本文介绍了Excel VBA:搜索字符串以找到第一个非文本字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单元格包含字符串中的字符混合,例如:

Cells contain a mixture of characters within a string, such as:


Abcdef_8765

QWERTY3_JJHH

Xyz9mnop

Abcdef_8765
QWERTY3_JJHH
Xyz9mnop

我需要找到第一个非 A-Za-z 字符,以便我可以删除字符串的后续剩余部分。

I need to find the first non A-Za-z character so that I can strip out the subsequent remainder of the string.

所以结果将是:


Abcdef

QWERTY

Xyz

Abcdef
QWERTY
Xyz

如果我确切知道我正在寻找什么角色,我知道如何做到这一点,但是我并不直观地掌握如何找到除 A-Za-z之外的任何角色

I know how to do this if I know exactly what character I'm looking for, but I'm not intuitively grasping how to find ANY character other than A-Za-z.

Btw,这是打算在一个 vba 解决方案。

Btw, this is intended to be used within a vba solution.

================ ==

====================

编辑:

以下...

a = "abc123"
b = Len(a)

For x = 1 To b

c = (Mid(a, x, 1) Like "[a-zA-Z]")
If c = False Then
d = Left(a, x - 1)
Exit Sub
End If

Next x

我是否绊倒了一个合适的解决方案,或者这是注定要打破?
我只是因为看了Doug Glancy的解决方案,而且看起来更加实质。
(btw,我还没有测试Doug的解决方案)

推荐答案

regexp 下面看起来要从第一个非 AZ 字符。

The regexp below looks to remove from the first non A-Z character.

Function StrChange(strIn As String) As String
Dim objRegEx As Object

Set objRegEx = CreateObject("vbscript.regexp")
With objRegEx
    .ignorecase = True
    .Pattern = "^([a-z]+)([^a-z].*)"
    .Global = True
     StrChange = .Replace(strIn, "$1")
End With
End Function

这篇关于Excel VBA:搜索字符串以找到第一个非文本字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆