检查列中的值以更改多个行值

我有一个数据量很大的电子表格(超过10万行列啊)。

我知道F列中有二十多个不同的值是错误的,我知道他们应该是什么。 例如,每次出现“pif”应该是“pig”,“coe”应该是“牛”等。

(请注意,每个都有多个不正确的值(即多个“pif”)。)

我目前正在构build一个macros来解决这些问题,这部分工作:

Sub FixColumnF() ActiveSheet.Columns("F").Replace What:= _ "pif", _ Replacement:="pig", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _ False, SearchFormat:=False, ReplaceFormat:=False ActiveSheet.Columns("F").Replace What:= _ "coe", _ Replacement:="cow", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:= _ False, SearchFormat:=False, ReplaceFormat:=False ... End Sub 

我的问题是,列A用于跟踪错误,其中之一是在F列中的一个不正确的值。如何擦除列A中的值,以指示列中的值的每一行不再有错误F是固定的?

我是非常新的vba,所以任何帮助将非常感激!

Siddharth在testing/打字时发表了他的build议。 我的第一个build议是他的第一个build议的一个简单的变化,并没有提供我可以看到的优势。 但是,我的第二个build议与他的任何一个build议都不一样,可能是合适的。

如果你想对每个发现的错误值做一些额外的事情,你需要循环每次出现“pif”和“coe”。 下面的代码显示了如何用“pig”replace“pif”的每一个出现,然后用列“A”做一些事情。 如果你喜欢这种技术,你需要复制这个代码“coe”和“cow”。

 Option Explicit Sub ReplaceFaultyA() Dim Rng As Range ' It is best to avoid "ActiveSheet" in case the use has called the ' macro with the wrong worksheet active. Probably this is not likely ' in this case but I like to follow good practice even if it not not ' necessary With Worksheets("Data") Do While True ' Are you sure all your Find parameters are correct? For example, ' "LookAt:=xlPart" means than "pif" in the middle of a word will ' be replaced by "pig". "LookAt:=xlWhole" may better match your ' requirement. I suggest you look up the specification of Find ' and consider the implications of each parameter. Set Rng = .Columns("F").Find(What:="pif") If Rng Is Nothing Then ' No [more] occurrences of "pif" in column F Exit Do Else ' Occurrences of "pif" found in column F .Cells(Rng.Row, "F") = "pig" ' Amend column ""A" of Rng.Row as necessary End If Loop End With End Sub 

如果只有两个替代品,复制我的循环并且用“coe”和“cow”replace“pif”和“pig”可能是最简单的解决scheme。 但是,如果有很多替代品,下面的技术可能是更好的select。

在这段代码中,我把错误的值和匹配好的值放在数组中。 采用这种方法,一个replace代码块可以处理不确定数量的replace,只要列A的动作与每个replace相同。

 Sub ReplaceFaultyB() Dim InxValue As Long Dim Rng As Range Dim ValueFaulty() As Variant Dim ValueGood() As Variant ' The code below assumes there same number of entries in both arrays. ' You can add extra entries to the two arrays as necessary. ' This assumes the action for column "A" is the same for each faulty value ValueFaulty = Array("pif", "coe", "appme") ValueGood = Array("pig", "cow", "apple") With Worksheets("Data") For InxValue = LBound(ValueFaulty) To UBound(ValueFaulty) Do While True Set Rng = .Columns("F").Find(What:=ValueFaulty(InxValue)) If Rng Is Nothing Then ' No [more] occurrences of this faulty value in column F Exit Do Else ' Occurrences of faulty value found in column F .Cells(Rng.Row, "F") = ValueGood(InxValue) ' Amend column ""A" of Rng.Row as necessary End If Loop Next InxValue End With End Sub 

有两种方法我可以想到。

方法1

使用.Find/.FindNext来循环所有的单元格,并说“pif”。 在该循环中,不仅可以replace单词,还可以编辑列A中的值。例如(未testing)

 Sub Sample() Dim oRange As Range, aCell As Range, bCell As Range Dim ws As Worksheet Dim SearchString As String, FoundAt As String On Error GoTo Whoa Set ws = Worksheets("Sheet1") Set oRange = ws.Columns(5) '<~~ Column 5 SearchString = "pif" NewString = "pig" Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then Set bCell = aCell '~~> Change value in Cell F aCell.Value = Replace(aCell.Value, SearchString, NewString) '~~> Change value in Cell A aCell.Offset(, -5).Value = "Rectified" Do Set aCell = oRange.FindNext(After:=aCell) If Not aCell Is Nothing Then If aCell.Address = bCell.Address Then Exit Do aCell.Value = Replace(aCell.Value, SearchString, NewString) aCell.Offset(, -5).Value = "Rectified" Else Exit Do End If Loop End If Exit Sub Whoa: MsgBox Err.Description End Sub 

方式2

使用自动filter来过滤单词“pif”上的Col F。 循环列A中的所有单元格并添加相关消息。 删除自动filter,然后运行你的原代码(在你的问题中提到)做列F中的replace。请参阅这个链接如何使用自动filter。