Excel VBA Replace Text with Two Arrays

て烟熏妆下的殇ゞ 提交于 2021-01-27 12:22:19

问题


I would like to replace the values in column A:A using arrays:

Dim aValueNew() As String
Dim aValueOld() As String

aValueNew = Split("ABC,DEF,GHI", ",")
aValueOld = Split("123,456,789", ",")

123 needs to be replaced by ABC, 456 by DEF and so forth.

What is the most efficient way of doing this? I am struggling on how to include the Replace function in a loop and your help would be appreciated. Something like:

For i = 0 to i = 2
Range("A:A").Replace What:= aValueOld(i), Replacement:=aValueNew(i), LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False

回答1:


You almost had it. You may find the documentation for For...Next helpful.

Dim i As Long
For i = 0 To 2
    Range("A:A").Replace What:=aValueOld(i), Replacement:=aValueNew(i), _
        LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
Next



回答2:


Try this code:

For i = 0 to UBound(aValueOld)
    Columns("A:A").Select
    Selection.Replace What:= aValueOld(i), Replacement:=aValueNew(i), LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False

Note:
Your For statement was wrong
If count of values are the bound of array use UBound(<Array Name>)



来源:https://stackoverflow.com/questions/29139230/excel-vba-replace-text-with-two-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!