【问题标题】:Remove part of text from all columns in an excel file从 excel 文件的所有列中删除部分文本
【发布时间】:2014-10-17 23:36:15
【问题描述】:

我有一个 excel 文件,其中有一列显示 Web 源 URL。数据长这样……

http://carter.mydomain.com/page1

http://expert.mydomain.com/page4

http://10629.mydomain.com/sample

http://genius.mydomain.com/form-1

我需要做的是删除之前和包括 (http://) 以及第一个 (.) 之后的所有内容 所以在前面的例子中,我想在列中留下以下数据

卡特

专家

10629

天才

在此先感谢您提供任何帮助。 凯伦

【问题讨论】:

    标签: excel text


    【解决方案1】:

    您可以使用这样的公式组合。这是假设 A1 包含第一行,其中有 Carter。

    =MID(A1,FIND("//",A1)+2,FIND(".",A1,FIND("//",A1))-FIND("//",A1)-2)
    

    【讨论】:

      【解决方案2】:

      这是一个使用正则表达式搜索/替换的蛮力宏,遗憾的是它不会自动成为 Excel 内置的搜索/替换功能的一部分。提示搜索/替换模式而不是硬编码会更有用,但您会明白这一点,因为这只是使用正则表达式的一个示例。请注意,您可能必须先将正则表达式引用添加到您的工作簿中。

      若要使用,请选择范围,添加后运行宏。

      Sub SearchReplaceRegex()
        Dim reg As New RegExp  ' regex object
        Dim searchPattern1     ' look for this
        Dim searchPattern2
        Dim replacePattern1    ' replace with this
        Dim replacePattern2
        Dim matchCell As Range ' selected cell
      
        ' Set search/replace patterns
        searchPattern1 = "^http://" ' http:// at the beginning of the line
        searchPattern2 = "\..*$"    ' 1st period followed by anything until the end of the line
        replacePattern1 = ""        ' replace with nothing
        replacePattern2 = ""
      
        ' regex object settings
        reg.IgnoreCase = True
        reg.MultiLine = False
      
        ' Loop through each selected cell
        For Each matchCell In Selection
      
          ' Does it match the first pattern?
          reg.Pattern = searchPattern1
          If reg.Test(matchCell) Then
            ' If so, replace the matched search pattern with the replace pattern
            matchCell = reg.Replace(matchCell, replacePattern1)
          End If
      
          ' Change to the second pattern and test again
          reg.Pattern = searchPattern2
          If reg.Test(matchCell) Then
            matchCell = reg.Replace(matchCell, replacePattern2)
          End If
      
        Next
      
      End Sub
      

      有关更多信息,请参阅这篇包含大量信息的帖子:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

      【讨论】:

      • 您可以在 excel 中使用替换功能并使用 Find what:.mydomain.com/* 并将其替换为空字符串,然后再执行一次 find what:http:// 并再次替换使用空字符串,不需要所有的 vba
      • 说得对,蚱蜢,但那样你就不会接触到正则表达式的力量了! :-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多