使用Excel Vba分割字符串,仅恢复[]之间的元素 [英] Split a string with Excel Vba and recover only an element between [ ]

查看:92
本文介绍了使用Excel Vba分割字符串,仅恢复[]之间的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为

"word 1, word 2, ..., word n, [Etiquette], many other words"

我只想检索礼节"并将其放入变量中.

I would like to retrieve only "Etiquette" and put it in a variable.

目前我有v = "blabla, [Etiquette]"

我知道我可以使用拆分"功能,但是当我尝试显示Debug.Print Split(v)时,出现类型错误.

I know I can use the function Split but when I just try to display Debug.Print Split(v) I get a type error.

谢谢您的帮助!

推荐答案

这是一种方法:

1,Filter(Split(x, ","), "[")获取包含"[...]"的字符串

1, Filter(Split(x, ","), "[") get the strings contain " [...]"

2,用join function将这些字符串连接为一个(这样可以避免过滤器什么也不返回时发生的错误)

2, Join these strings into one by join function (this avoids errors that occur when the filter return nothing)

3,用replace function

4,通过trim function

. 这是测试代码:

Sub Test()
x = "word 1, word 2, ..., word n, [Etiquette], many other words"
z = Trim(Replace(Replace(Join(Filter(Split(x, ","), "["), ","), "[", ""), "]", ""))
'z = Replace(Join(Filter(Split(Replace(x, "[", "]#"), "]"), "#"), ","), "#", "")'vs2
Debug.Print Z
End Sub

这篇关于使用Excel Vba分割字符串,仅恢复[]之间的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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