【发布时间】:2018-12-25 13:39:21
【问题描述】:
我有下面的(简化的)示例,我试图让 vba 在 excel 中为我做一些事情。有 3 列,第二列和第三列可能有不同的标题,但数据基本相同。我想保留这两列。
我想仅在第二列中查找某些内容,然后替换具有我正在搜索的值的行的第一列中的值。因此,作为一个简单的示例,我将仅在第 2 列中搜索所有“505”,然后将这些对应行的第 1 列替换为“A”。
请注意,这个庞大的电子表格及其数据每天都在变化,因此没有固定的行数或“505”的频率。所以我需要这个循环。此外,即使大部分数据是重复的,我也需要保留第 2 列和第 3 列。有人可以提供一种简单而强大的方法吗?提前致谢!
TYPE ID Model
E 505 505
E 505 505
E 505 505
E 505 505
E 606 606
E 606 606
E 606 606
E 606 606
代码:
Sub searchrange()
'
' searchrange Macro
'
Dim searchrange As Range
Range("A1").Select
Cells.Find(What:="id", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
'this below line is what I am having trouble with; I need to get the (active, or certain) column to be defined as the search range.
searchrange = ActiveCell.EntireColumn.Select
Selection.Find(What:="606", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.Offset(0, -1).FormulaR1C1 = "A"
Cells.FindNext(After:=ActiveCell).Activate
Selection.Offset(0, -1).FormulaR1C1 = "A"
End Sub
【问题讨论】:
标签: excel vba search replace range