How to change case of matching letter with a VBA regex Replace?

前端 未结 2 1949
余生分开走
余生分开走 2021-01-25 01:40

I have a column of lists of codes like the following.

2.A.B, 1.C.D, A.21.C.D, 1.C.D.11.C.D
6.A.A.5.F.A, 2.B.C.H.1
8.ABC.         


        
2条回答
  •  难免孤独
    2021-01-25 02:04

    So this question is old, but I do have another workaround. I use a double regex so to speak, where the first engine looks for the match as an execute, then I loop through each of those items and replace with a lowercase version. For example:

    Sub fixBlah()
    Dim re As VBScript_RegExp_55.RegExp
    dim ToReplace as Object
    Set re = New VBScript_RegExp_55.RegExp
    for each c in Selection.Cells
    with re      `enter code here`
      .Global = True
      .Pattern = "\b([1-5]\.[A-Z])\.([A-Z])\b"
      Set ToReplace = .execute(C.Value)
    end with
    
     'This generates a list of items that match.  Now to lowercase them and replace
     Dim LcaseVersion as string
     Dim ItemCt as integer
     for itemct = 0 to ToReplace.count - 1
     LcaseVersion = lcase(ToReplace.item(itemct))
          with re      `enter code here`
      .Global = True
      .Pattern = ToReplace.item(itemct)  'This looks for that specific item and replaces it with the lowercase version
      c.value = .replace(C.Value, LCaseVersion)
    end with
    End Sub
    

    I hope this helps!

提交回复
热议问题