vba 返回字符串中第 N 个字符之后的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18878151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:38:52  来源:igfitidea点击:

Return the characters after Nth character in a string

excelvba

提问by user2683996

I need help! Can someone please let me know how to return the characters after the nth character?

我需要帮助!有人可以让我知道如何返回第 n 个字符之后的字符吗?

For example, the strings I have is "001 baseball" and "002 golf", I want my code to return baseball and golf, not the number part. Since the word after the number is not always the same length, I cannot use = Right(String, n)

例如,我拥有的字符串是“001 棒球”和“002 高尔夫”,我希望我的代码返回棒球和高尔夫,而不是数字部分。由于数字后面的单词并不总是相同的长度,我不能使用 = Right(String, n)

Any help will be greatly appreciated

任何帮助将不胜感激

回答by Alex P

If your numbers are always 4 digits long:

如果您的号码总是 4 位数:

=RIGHT(A1,LEN(A1)-5) //'0001 Baseball' returns Baseball

If the numbers are variable (i.e. could be more or less than 4 digits) then:

如果数字是可变的(即可能多于或少于 4 位数字),则:

=RIGHT(A1,LEN(A1)-FIND(" ",A1,1)) //'123456 Baseball' returns Baseball

回答by rory.ap

Mid(strYourString, 4)(i.e. without the optional length argument) will return the substring starting from the 4th character and going to the end of the string.

Mid(strYourString, 4)(即没有可选的长度参数)将返回从第 4 个字符开始到字符串末尾的子字符串。

回答by tigeravatar

Alternately, you could do a Text to Columns with space as the delimiter.

或者,您可以使用空格作为分隔符执行 Text to Columns。

回答by Aaron Thomas

Since there is the [vba] tag, split is also easy:

由于有 [vba] 标签,拆分也很容易:

str1 = "001 baseball"
str2 = Split(str1)

Then use str2(1).

然后使用 str2(1)。

回答by barry houdini

Another formula option is to use REPLACE function to replace the first n characters with nothing, e.g. if n = 4

另一个公式选项是使用 REPLACE 函数将前 n 个字符替换为空,例如,如果 n = 4

=REPLACE(A1,1,4,"")

=REPLACE(A1,1,4,"")