vba 在最后一个反斜杠后提取剩余字符串的函数

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

Function to extract remaining string after last backslash

excelexcel-vbareplaceexcel-formulafind-replacevba

提问by user664481

I need an Excel function that can extract a string after last \from a path and if no \found then take the whole string. For example:

我需要一个 Excel 函数,它可以\从路径中的last 之后提取一个字符串,如果没有\找到,则获取整个字符串。例如:

D:\testing\rbc.xls                     output will be   rbc.xls
D:\home\testing\test1\script1.sql      output will be   script.sql
script 3.txt                           output will be   script 3.txt

回答by Davesexcel

1.Change all the "\" to spaces, the number of spaces is determined by the number of characters in the cell

1.将“\”全部改成空格,空格数由单元格字符数决定

2.Use the right function to extract the right of the string based on the number of characters in the cell.

2.使用right函数根据单元格中的字符数提取字符串的右边。

3.Use the trim function to remove the spaces.

3.使用修剪功能去除空格。

enter image description here

在此处输入图片说明

Your results will be.

你的结果将是。

enter image description here

在此处输入图片说明

=TRIM(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",LEN(A1))),LEN(A1)))

=TRIM(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",LEN(A1))),LEN(A1)))

As suggested, one way to do this without formulas or vba would be to use "Find/Replace". Hit Ctrl & "H" keys and do the following.

正如所建议的,在没有公式或 vba 的情况下执行此操作的一种方法是使用“查找/替换”。按 Ctrl 和“H”键并执行以下操作。

Find what *\ and replace with nothing

查找 *\ 并用空替换

enter image description here

在此处输入图片说明

The VBA code for that would be

该 VBA 代码将是

Sub ReplaceIt()
    Columns("A").Replace What:="*\", Replacement:="", SearchOrder:=xlByColumns, MatchCase:=True
End Sub