简体   繁体   English

替换多列中的值

[英]Replacing values in multiple columns

I am new to vba so bear with me. 我是vba的新手,请多多包涵。

I need to replace values in multiple columns in a worksheet. 我需要替换工作表中多个列中的值。 There are about 50 values that need to be matched and replaced if present. 大约有50个值需要匹配和替换(如果存在)。

For example: I want to search in column C and F to see if any of my listed values exist and then replace it with another text. 例如:我想搜索C和F列以查看是否存在我列出的任何值,然后将其替换为另一个文本。 Every searched value have a unique replacement. 每个搜索到的值都有一个唯一的替换项。

Preferably if the string can search and replace both columns with the same lookup. 最好是该字符串可以搜索并用相同的查找替换两列。 Not have several statements for every column. 每列没有几个语句。

This is what I tried to use: 这是我尝试使用的方法:

Sub reppp()

    Columns("C:C").Replace What:="Search1", Replacement:="Text1", SearchOrder:=xlByColumns
    Columns("F:F").Replace What:="Search1", Replacement:="Text1", SearchOrder:=xlByColumns
    Columns("C:C").Replace What:="Search2", Replacement:="Text2", SearchOrder:=xlByColumns
    Columns("F:F").Replace What:="Search2", Replacement:="Text2", SearchOrder:=xlByColumns

End Sub

As it is I need to change/add 2 entries for every "replace". 因为它是我需要为每个“替换”更改/添加2个条目。

I also get new work sheets which need this treatment regulary. 我也得到新的工作表,需要定期进行这种处理。 What is the easiest way to convert these sheets to my specifications? 将这些工作表转换为我的规格的最简单方法是什么? Is there a way to state the search and replacements in a separate worksheet and somehow call that document and run the macro? 有没有办法在单独的工作表中声明搜索和替换项,并以某种方式调用该文件并运行宏?

This should meet your needs: 这应该满足您的需求:

Sub demo()
    Dim r As Range
    Set r = Range("C:C, F:F")
    ary1 = Array("Search1", "Search2")
    ary2 = Array("Text1", "Text2")
    For i = 0 To 1
        r.Replace What:=ary1(i), Replacement:=ary2(i)
    Next i
End Sub

Note: 注意:

You can increase the arrays and modify the For statement to expand the translation table. 您可以增加数组并修改For语句以扩展转换表。

Following the logic of my post here , here's a code that suits your needs. 继我的文章的逻辑在这里 ,这里是一个适合您需要的代码。

Sub FindReplaceWithRef()

    Dim Wbk As Workbook: Set Wbk = ThisWorkbook
    Dim Wsht As Worksheet: Set Wsht = Wbk.Sheets("Sheet1") 'Modify as needed.
    Dim Dict As Object
    Dim RefList As Range, RefElem As Range
    Dim TargetRng As Range

    Set Dict = CreateObject("Scripting.Dictionary")
    Set RefList = Wsht.Range("J1:J3") 'Modify as needed.
    Set TargetRng = Union(Wsht.Range("C1:C20"), Wsht.Range("F1:F20")) 'Modify as needed.

    With Dict
        For Each RefElem In RefList
            If Not .Exists(RefElem) And Not RefElem Is Nothing Then
                .Add RefElem.Value, RefElem.Offset(0, 1).Value
            End If
        Next RefElem
    End With

    For Each Key In Dict
        With TargetRng
            .Replace What:=Key, Replacement:=Dict(Key)
        End With
    Next Key

    Set Dict = Nothing

End Sub

Screenshots: 截图:

Before running code:

在此处输入图片说明

After running code:

在此处输入图片说明

Let us know if this helps. 让我们知道是否有帮助。

If your table is set up without gaps in the data, using the UsedRange is a good method for what you need. 如果您的表设置没有数据空白,则使用UsedRange是满足您需要的好方法。

Range("C:C,F:F").Select With ActiveSheet.UsedRange .Replace "Search1", "Text1", xlPart .Replace "Search2", "Text2", xlPart .Replace "Search3", "Text3", xlPart .Replace "Search4", "Text4", xlPart .Replace "Search5", "Text5", xlPart .Replace "Search6", "Text6", xlPart .Replace "Search7", "Text7", xlPart End With Range(“ C:C,F:F”)。使用ActiveSheet.UsedRange选择。替换“ Search1”,“ Text1”,xlPart。替换“ Search2”,“ Text2”,xlPart。替换“ Search3”,“ Text3”, xlPart .Replace“ Search4”,“ Text4”,xlPart .Replace“ Search5”,“ Text5”,xlPart .Replace“ Search6”,“ Text6”,xlPart .Replace“ Search7”,“ Text7”,xlPart结尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM