簡體   English   中英

檢查列中的值以更改多個行值

[英]Checking values in a column to change multiple row values

我有一個包含大量數據的電子表格(超過100,000行的列ah)。

我知道F列中有20個或更多不同的值是錯誤的,我知道它們應該是什么。 例如,每次出現的“ pif”應為“ pig”,“ coe”應為“ cow”,等等。

(請注意,每個都有多個不正確的值(即多個“ pif”)。)

我目前正在建立一個宏來逐一解決這些問題,並且該部分有效:

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的新手,所以非常感謝您的幫助!

我可以想到兩種方法。

方式1

使用.Find/.FindNext循環遍歷所有帶有單詞“ pif”的單元格。 在該循環中,不僅要替換單詞,而且還要在Col A中編輯值。例如(未經測試)

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

方式二

使用自動過濾器過濾單詞“ pif”上的ColF。 遍歷Col A中的所有單元格並添加相關消息。 刪除自動篩選,然后運行你的原代碼(在你的問題中提到)做在山口F.替換見關於如何使用自動篩選鏈接。

我在測試/鍵入我的內容時,Siddharth發表了他的建議。 我的第一個建議是他的第一個建議的簡單變體,沒有我看到的優勢。 但是,我的第二個建議不同於他的兩個建議,可能是適當的。

如果要對發現的每個錯誤值進行額外的處理,則需要為“ pif”和“ coe”的每次出現循環。 下面的代碼顯示了如何用“ pig”替換每次出現的“ 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”替換“ pif”和“ pig”可能是最簡單的解決方案。 但是,如果有很多替代品,則以下技術可能是更好的選擇。

在這段代碼中,我將錯誤值和匹配的良好值放置在數組中。 使用這種方法,如果每次替換的列A的操作相同,則替換代碼塊可以處理無限數量的替換。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM