gpt4 book ai didi

arrays - 如何使用数组查找/替换选定单元格中的多个值?

转载 作者:行者123 更新时间:2023-12-04 08:24:52 24 4
gpt4 key购买 nike

我正在尝试在 Excel 工作表名称“wsPivotPreCCI”中构建 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
所以,我调整了 RplcTips来自 ArrayString
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值(value),并且似乎继续寻找(直到我停止宏)。
问题1:替换值是否应该是 String而不是 Array ?
问题 2:替换 C 列中所有这些值的最佳方法是什么?

最佳答案

是的,重置值(value)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 有关参数的详细信息的文档。

关于arrays - 如何使用数组查找/替换选定单元格中的多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65310381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com