【问题标题】:Trying to Change a Select Number of Words in a Column Into a Different Word Excel VBA尝试将列中的选定单词数更改为不同的单词 Excel VBA
【发布时间】:2021-09-24 15:44:07
【问题描述】:

我目前正在尝试创建一个向下查看整个列的宏,如果该单词等于某个单词,则会将其更改为更合适的单词。

例如,每当动物“Bird”位于 B 列时,它就会变成“Parrot”这个词。如果在向下搜索 B 列时遇到单词“Dog”或“Cat”,则该单词将保持不变。

任何帮助将不胜感激!谢谢!!

【问题讨论】:

  • Range.Replace 在两个数组、一个 searchTerms 和一个 replaceTerms 上循环。或使用Scripting.Dictionary
  • 基本解决方案:Columns("B").Replace "Bird", "Parrot".

标签: excel vba for-loop if-statement


【解决方案1】:

试试这个:

Sub Findthenreplace()

    Dim myDataRng As Range
    Dim cell As Range

    ' set the range to 2nd column "B"
    Set myDataRng = Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    
    For Each cell In myDataRng
        If InStr(1, cell.Value, "Bird") > 0 Then
            cell.Value = Replace(cell.Value, "Bird", "Parrot")

        End If
    Next cell
    
End Sub

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多