【问题标题】:Wildcard use within Replace - Specific number of characters在替换中使用通配符 - 特定数量的字符
【发布时间】:2015-10-07 13:57:55
【问题描述】:

我有以下问题:

我想替换另一个工作表的链接。此链接在宏期间从“MeasData!E10”更改为“MeasData_XXX!E10”(XXX 任何数字),并且可以是宏期间的任何这些。现在我想用当前工作表的单元格替换其中之一。

问题是,我的单元格包含一个以上的字符串,例如:

=MeasData_110!E10*MeasData_110!E15*MeasData_110!E20

当使用 Cells.Replace 方法时,这将正确地将 MeasData_110!E10 替换为设置的字符串。但是,如果我要查找的链接不在第一个位置,例如:

=MeasData_110!E20*MeasData_110!E10*MeasData_110!E15

它将被替换为:

=STRING*MeasData_110!E15

因为我只是使用通配符:

Worksheets(1).Cells.Replace _
What:="MeasData*!E10", Replacement:=STRING

我还没有发现是否有通配符 a) 特定字母 和 b) 特定/可变数量的字母(0-4)

有人解决了吗?

【问题讨论】:

  • 唯一的通配符是*?。您可以使用正则表达式,但需要循环。

标签: vba excel wildcard


【解决方案1】:

我认为最快的方法是循环使用Replace()

Sub MM()
Dim foundCell       As Excel.Range
Dim foundAddress    As String
Dim findString      As String
Dim replaceString   As String

findString = "MeasData!E10"
replaceString = Range("AD48").Value

Set foundCell = Sheets(1).Cells.Find(What:=findString, LookIn:=xlFormulas, LookAt:=xlPart)

If Not foundCell Is Nothing Then

foundAddress = foundCell.Address
    Do
        With foundCell
            .Formula = Replace(.Formula, findString, replaceString)
        End With

        Set foundCell = Sheets(1).Cells.FindNext(foundCell)

    Loop While Not foundCell Is Nothing

End If

End Sub

或者

如果您愿意,可以通过后期绑定使用 VBScript.RexExp 对象,如下所示:

Function ReplaceAllWith(old_string As String, pattern As String, new_string As String) As String
    With CreateObject("VBScript.RegExp")
        .pattern = pattern
        .Global = True
        If .Test(old_string) Then
            ReplaceAllWith = .Replace(old_string, new_string)
        Else
            ReplaceAllWith = old_string
        End If
    End With
End Function

将上面的内容插入到你的模块中,然后像这样使用:

For Each cell In Sheets(1).UsedRange.Cells
    If cell.HasFormula Then
        cell.Formula = ReplaceAllWith(cell.Formula, "MeasData(_[\d]{3})?!E10", Range("AD48").Value)
    End If
Next

【讨论】:

  • 第二个看起来很有趣。但我想查找“Measdata!E10”和“Measdata_123!E10”,并用单元格“AD48”替换两者中的任何一个(如果使用它们)。你刚才写的用“MeasData_123!E10”替换了“MeasData!E10”?!
  • 就像一个魅力,搜索词的正则表达式必须是这样的:MeasData(_?[\d]{0,3})!E10
  • 不应该是MeasData(_[\d]{1,3})?!E10否则会匹配没有数字的下划线。 See this example
【解决方案2】:

如果你知道手机号码,你可以使用下面的

动态传递变量 Cells1、cells2 和 cells3 的值

cells1 = "110!E10"

cells2 = "110!E15"

Cells3 = "110!E20"

str1 = "=MeasData_" & cells1 & "*Measdata_" & cells2 & "*MeasData_" & Cells3

'Debug.Print str1 '打印并验证是否需要

【讨论】:

  • 这根本没有帮助。我想替换整个字符串?!
【解决方案3】:

你试过正则表达式吗?您需要为此添加对 Microsoft VBScript 正则表达式 5.5 的引用

    Sub test()
    Dim a As String
    a = "=MeasData_110!E20*Measdata_110!E10*MeasData_110!E15*Measdata_123!E10"
    a = ReplaceNumbers(a, "MeasData_STRING!E10")
    MsgBox a
    End Sub
    Private Function ReplaceNumbers(inputString As String, replacement As String) As String
        Pattern = "Meas[dD]ata_([0-9]{1,})\!E10"
        output = inputString
         Dim re As New RegExp
        re.Pattern = Pattern
        re.Global = True: re.MultiLine = True
        If re.test(inputString) Then
            ReplaceNumbers = re.Replace(inputString, replacement)
        Else
            ReplaceNumbers = inputString
        End If
    End Function

【讨论】:

  • 明天试试这个。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
相关资源
最近更新 更多