vba很难找到替换

vba很难找到替换,vba,excel,replace,find,Vba,Excel,Replace,Find,我有点困惑,如果我当前的方法是最好的,但我有excel文件,其中第一行有不同的列标题,这取决于我从谁那里获得它们。我正在尝试标准化第1行中的标题 我该如何编写脚本来查找,比如: find=“clname”或“first name”或“full name” replace=“Name” find=“address”或“adr”或“location” replace=“Cl\u地址” 我将需要做大约15个不同的发现和更换。有没有更好的办法。我目前正在使用这个脚本 Sub findrep()

我有点困惑,如果我当前的方法是最好的,但我有excel文件,其中第一行有不同的列标题,这取决于我从谁那里获得它们。我正在尝试标准化第1行中的标题

我该如何编写脚本来查找,比如:

find=“clname”或“first name”或“full name”
replace=“Name”

find=“address”或“adr”或“location”
replace=“Cl\u地址”

我将需要做大约15个不同的发现和更换。有没有更好的办法。我目前正在使用这个脚本

Sub findrep()
    Dim i As String
    Dim k As String

    i = "Find"
    k = "Text to replace"
    Rows(1).replace what:=i, replacement:=k, lookat:=xlPart, MatchCase:=False
End Sub

使用大小相同的两个变体阵列;一个用于
what:=
值,另一个用于
替换:=
值。循环浏览它们以完成替换

Sub blahblah()
    Dim v As Long, fnd As Variant, rpl As Variant

    fnd = Array("clname", "first name", "full name", _
                "address", "adr", "location")
    rpl = Array("name", "name", "name", _
                "Cl_Adress", "Cl_Adress", "Cl_Adress")

    With Worksheets("Sheet1")
        With .Rows(1)
            For v = LBound(fnd) To UBound(fnd)
                .Replace what:=fnd(v), replacement:=rpl(v), _
                         MatchCase:=False, lookat:=xlWhole
            Next v
        End With
    End With
End Sub

我会用正则表达式。将引用设置为“Microsoft VBScript正则表达式5.5”,然后在第一行的列中循环执行此操作

子项替换多个项() Dim rx1作为新的RegExp Dim rx2作为新的RegExp Dim intColCount为整数 作为整数的Dim intColCounter

intColCount = ActiveSheet.UsedRange.Columns.Count

rx1.Pattern = "clname|first name|full name"
rx2.Pattern = "address|adr|location"

For intColCounter = 1 To intColCount
    ActiveSheet.Cells(1, intColCounter).Value = _
    rx2.Replace(rx1.Replace(ActiveSheet.Cells(1, intColCounter).Value, "Name"), "Cl_Adress")
Next intColCounter

End Sub

是否确实要查看:=xlPart?在我看来,列标题标签应该是整个单元格内容。