【问题标题】:How to Find / Replace Multiple Values within Selected Cells using Arrays?如何使用数组查找/替换选定单元格中的多个值?
【发布时间】:2021-03-26 08:29:11
【问题描述】:

我正在尝试在名为“wsPivotPreCCI”的 Excel 工作表中构建 VBA 宏。

在“C”列中,我想查找多个文本字符串,并将其替换为相同的文本字符串。
示例:

Find: "TIPS TELEPHONE", "TIPS TELEPHONE OTHER", "TIPS, TELEPHONE", or "TIPS,TELEPHONE"
Replace All with: "TIPS, TELEPHONE, OTHER"

经过数小时的研究和多次尝试(如下所述),我发现这些帖子最有帮助,但我似乎仍然无法正确使用循环和替换。

VBA find/replace using array for cell range

find and replace values in database using an array VBA

我的查找/替换数组是:

FindTips = Array("TIPS TELEPHONE", "TIPS TELEPHONE OTHER", "TIPS, TELEPHONE", "TIPS,TELEPHONE")
RplcTips = Array("TIPS, TELEPHONE, OTHER")
        
FindAuto = Array("AUTO - RENTAL, PARKING & TOLLS", "PARKING AND TOLLS", "PARKING TOLLS", _
"RENTAL PARKING & TOLLS", "RENTAL PARKING TOLLS", "AUTO RENTAL, PARKING & TOLLS")
RplcAuto = "AUTO - RENTAL, PARKING & TOLLS"
        
FindMisc = Array("MISCELLANEOUS EXPENSE", "MISCELLANEOUS")
RplcMisc = "MISCELLANEOUS EXPENSE"
        
FindTraining = Array("TRAINING & SEMINARS", "TRAINING & SEMINARS-OTHERS")
RplcTraining = "TRAINING & SEMINARS"

这是我当前的代码。我只是一个查找/替换数组示例(但我确实需要替换所有数组):

Options Explicit
Sub Multi_FindReplace()

With wsPivotPreCCI
    Dim Lrow As Long
    Dim Rng As Range
    Dim FindTips As Variant
    Dim RplcTips As Variant
    Dim i As Long
    
    Lrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set Rng = .Range("C2:C" & Lrow)
    
    FindTips = Array("TIPS TELEPHONE", "TIPS TELEPHONE OTHER", "TIPS, TELEPHONE", "TIPS,TELEPHONE")
    RplcTips = Array("TIPS, TELEPHONE, OTHER")
    
    'Loop through each item in array list

End With
End Sub

这些是我尝试使用的不同行,错误出现在发生错误的行上。

    For i = LBound(FindTips) To UBound(FindTips)
        Rng.Cells.Replace What:=FindTips(i), Replacement:=RplcTips(i)  '<-Subscript Out of Range
    Next i

    For i = LBound(FindTips) To UBound(FindTips)
        .Cells.Replace FindTips(i, 1), RplcTips(i, 1), xlWhole, xlByRows   '<-Subscript out of Range
    Next

    Dim arr As Variant
    For i = LBound(FindTips) To UBound(FindTips)
        For Each arr In Rng
            Rng.Cells.Replace What:=FindTips(i), Replacement:=RplcTips(i)  '<-Subscript out of Range
        Next arr
    Next i

    With Rng
        For i = LBound(FindTips) To UBound(FindTips)
            .Cells.Replace FindTips(i), RplcTips(i)  '<-Subscript out of Range
        Next
    End With

'Finally Looped through Column "C", but All the cells changed, and then received Subscript out of range
    For i = LBound(FindTips) To UBound(FindTips)
        For Each FindTips In Rng
            Rng.Cells.Replace What:=FindTips(i), Replacement:=RplcTips(i)
        Next FindTips
    Next i

在最后一次尝试之后,我发现了这个帖子: VBA (Microsoft Excel) replace Array with String

所以,我将 RplcTipsArray 调整为 String

Options Explicit
Sub Multi_FindReplace()

With wsPivotPreCCI
    Dim Lrow As Long
    Dim Rng As Range
    Dim FindTips As Variant
    Dim RplcTips As String
    Dim i As Long
    
    Lrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set Rng = .Range("C2:C" & Lrow)
    
    FindTips = Array("TIPS TELEPHONE", "TIPS TELEPHONE OTHER", "TIPS, TELEPHONE", "TIPS,TELEPHONE")
    RplcTips = "TIPS, TELEPHONE, OTHER"
    
    'Loop through each item in array list
    For i = LBound(FindTips) To UBound(FindTips)
        For Each FindTips In Rng
            Rng.Cells.Replace FindTips(i), RplcTips
        Next FindTips
    Next i

End With
End Sub

此代码仍将每个单元格(在 C 列中)更改为 Rplctips 值,并且似乎继续查找(直到我停止宏)。

问题 1:替换值应该是 String 而不是 Array

问题 2:替换 C 列中所有这些值的最佳方法是什么?

【问题讨论】:

    标签: arrays excel vba loops


    【解决方案1】:

    是的,替换值 RplcTips 应该是 String,因为您处理的是一个值,而不是一组值。

    然后,您不需要循环替换单元格。在整个范围内致电Replace。改变

    For i = LBound(FindTips) To UBound(FindTips)
        For Each FindTips In Rng
            Rng.Cells.Replace FindTips(i), RplcTips
        Next FindTips
    Next i
    

    For i = LBound(FindTips) To UBound(FindTips)
        Rng.Replace FindTips(i), RplcTips, xlWhole, xlByColumns, True
    Next i
    

    有关参数的详细信息,请参阅Range.Replace 文档。

    【讨论】:

    • 谢谢!!有没有办法(合并)添加其他查找/替换数组?还是我需要为这些做单独的循环?
    • 您可以重构您的子例程以采用两个参数:查找数组和替换字符串。
    • 好的,我会查一下。如果我被卡住了,我应该把它作为一个新问题提交吗?
    • 当然。作为开始,Sub MultiFindReplace(ByVal findArray As Variant, ByVal replacement As String)。然后在 new 子例程中,您将执行类似MultiFindReplace FindMisc, RplcMisc 的操作
    猜你喜欢
    • 2022-07-29
    • 2023-01-31
    • 2012-11-05
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    相关资源
    最近更新 更多