vba 如何使用VBA在字符串中查找字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44762411/
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-12 12:47:42  来源:igfitidea点击:

How to find a character in a string using VBA

excelvbaworksheet-function

提问by Ashwin

I have getting passed from another funtion, foldername like foldername = "a\\b\\c"or foldername = "a"and i'm trying to find the folder name contains "\\"and substitute "\\"with "__", split foldername based on "__"and pass into an array.

我已经从另一个函数传递过来,像foldername = "a\\b\\c"or一样foldername = "a"的文件夹名称,我试图找到文件夹名称包含"\\"并替换"\\""__",基于拆分文件夹名称"__"并传递到数组中。

please have a look at what i was trying till now.

请看看我到现在为止都在尝试什么。

sample values:

样本值:

If WorksheetFunction.Find("\", foldername) = 1 Then
       foldername  = WorksheetFunction.Substitute(foldername, "\", "__")
       SheetNames() = Split(foldername, "__")
End If

i'm getting the below error.

我收到以下错误。

enter image description here

在此处输入图片说明

回答by Ashwin

Dim a as Integer

'consider current value of is `foldername  = "a\b\c"`

a = InStr(foldername, "\")

'if InStr is not able to find the value funtion returns 0

If a <> 0  Then
       foldername  = Replace(foldername, "\", "__")
       SheetNames() = Split(foldername, "__")
End If

Thanks for your suggestions guys, appreciate it.

谢谢各位大佬的建议,谢谢。

回答by Nathan_Sav

SheetNames() = Split(foldername, "\")
foldername=join(sheetnames,"__")

This will do it.

这将做到。