string - VBA:如何在字符串中查找 "@"符号之后的值

标签 string vba

我正在尝试将 @ 符号后面的字母设置为变量。

例如,x = @BAL

我想设置 y = BAL

x = @NE

我想要y = NE

我正在使用 VBA。

最佳答案

Split() 在我看来是最简单的方法:

Dim myStr As String
myStr = "@BAL"
If InStr(, myStr, "@") > 0 Then '<-- Check for your string to not throw error
    MsgBox Split(myStr, "@")(1)
End If

正如 Scott Craner 明智地指出的那样,您应该检查以确保字符串包含他在 this comment 中检查的值通过这样做: y = Split(x,"@")(ubound(Split(x,"@"))。另一种方法是使用 InStr(): If InStr(, x, "@") > 0 Then...

(1) 将获取您要查找的字符的第一个实例之后的所有内容。如果您要使用 (0),那么这将占用 @ 之前的所有内容。


类似但不同的例子:

Dim myStr As String
myStr = "@BAL@TEST"

MsgBox Split(myStr, "@")(2)

消息框将返回 TEST,因为您使用了 (2),这是您的 @ 字符的第二个实例。


然后你甚至可以将它们拆分成一个数组:

Dim myStr As String, splitArr() As String
myStr = "@BAL@TEST"

splitArr = Split(myStr, "@")   '< -- don't append the collection number this time

MsgBox SplitArr(1)  '< -- This would return "BAL"
MsgBox SplitArr(2)  '< -- This would return "TEST"

如果您正在寻找更多阅读内容,请参阅 MSDN 中的更多内容:

Split Function

Description Returns a zero-based, one-dimensional array containing a specified number of substrings. SyntaxSplit( expression [ ,delimiter [ ,limit [ ,compare ]]] ) The Split function syntax has thesenamed arguments:

expression

Required. String expression containing substrings and delimiters. If expression is a zero-length string(""), Split returns an empty array, that is, an array with no elements and no data.

分隔符

Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.

限制

Optional. Number of substrings to be returned; -1 indicates that all substrings are returned.

比较

Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values.

关于string - VBA:如何在字符串中查找 "@"符号之后的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039453/

相关文章:

python - 从文本文件中的行创建字符串列表的大多数 pythonic 方法?

c++ - 我想计算字符串/c++ 中的数字

validation - Phalcon 框架 : Can't save not required strings in database

vba - 如何在 Visio 中使用 VBA 将形状添加到组中

Excel VBA : Late binding reference

php - 在 mail() 中使用的转义字符串

string - 可视化算法的工具

excel - 通过宏删除工作表

excel - 如何设置日期?

vba - 循环宏并随每个循环更改范围