vba 从 Excel 中的文本单元格中提取 URL

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

Extract URL's from a Cell of Text in Excel

excelvbaparsingexcel-formulaformula

提问by Chris Vasquez

I have an Excel document with a column that contains cells of text (small paragraphs usually). I'd like to find a way (preferably a formula) to extract any url's those cells by row and add them to another column.

我有一个 Excel 文档,其中有一列包含文本单元格(通常是小段落)。我想找到一种方法(最好是公式)来逐行提取任何 url 的这些单元格并将它们添加到另一列。

I've been messing around with MID and FIND quite a bit and can very easily get to the begginning of those URLs by searching for "http", but I can't figure out how to then find the length of the url so I can grab it.

我一直在搞 MID 和 FIND,通过搜索“http”可以很容易地找到这些 URL 的开头,但我不知道如何找到 url 的长度,所以我可以抓住它。

Really looking forward to any help anyone could offer. It's driving me nuts!

真的很期待任何人可以提供的任何帮助。快把我逼疯了!

采纳答案by guitarthrower

To take into account the URL happening at the end of a string you'll need to add some error handling.

要考虑发生在字符串末尾的 URL,您需要添加一些错误处理。

This should work for both mid string and end of string:

这应该适用于中间字符串和字符串结尾:

=MID(C11,FIND("http",C11),IFERROR(FIND(" ",C11,FIND("http",C11))-1,LEN(C11))-FIND("http",C11)+1)

回答by Chris Vasquez

Ok, I think I've got it working. Check it out:

好的,我想我已经开始工作了。一探究竟:

=MID(C11,FIND("http",C11),(FIND(" ",C11,FIND("http",C11))-FIND("http",C11))-4)

回答by durbnpoisn

Your most likely option is to make sure you can rely on the URLs being formed the same way. Like, always start with "http" and always end with "/" or a ".com". Then you can do formulas to find the index start, index end, and grab the MID for everything in between.

您最可能的选择是确保您可以依赖以相同方式形成的 URL。比如,总是以“http”开头,总是以“/”或“.com”结尾。然后,您可以使用公式来查找索引开始、索引结束并获取中间所有内容的 MID。

It should look like this:

它应该是这样的:

=FIND("http",D3)

returns the location of the first part.

返回第一部分的位置。

=FIND(".com",D3)

returns the location of the end

返回结束的位置

=MID(D3,D9,D10-D9+4)

Returns the string provided by the start to the end+4 (to allow for those 4 characters)

返回由开始到结束 +4 提供的字符串(以允许这 4 个字符)

回答by OTTA

Once you've identified the start position of the URL look for the first space after that, should be the end of the URL.

一旦您确定了 URL 的开始位置,请查找之后的第一个空格,应该是 URL 的结尾。