【问题标题】:find and replace values in excel using vba - lookup table overwritten使用vba查找和替换excel中的值 - 查找表被覆盖
【发布时间】:2021-11-10 19:18:45
【问题描述】:

我基本上使用这个问题find and replace values in database using an array VBA 的解决方案,它也可以正常工作。但是,几天以来,在执行代码时,还替换了表数组左列中的查找值,我不知道为什么会这样。 查找表在 tab_replace 工作表上称为 tab_replace。因此,每当代码在目标工作表上执行替换时,查找表第一列中的值也会被替换。

Sub Datastream_Code_Replacement()

Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant
Dim TempArray As Variant
Dim targetRange As Range
Dim X As Integer

Application.DisplayAlerts = False

  Set tbl = Worksheets("tab_replace").ListObjects("tab_replace")

  Set TempArray = tbl.DataBodyRange
  myArray = TempArray
  

  fndList = 1
  rplcList = 2

'Loop through each item in Array lists

    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
    ' Skip the request table, so that no Reuters Codes are replaced
      For Each sht In ActiveWorkbook.Worksheets
        If sht.Name <> "CodeReplacement" Then
        If sht.Name <> "tab_replace" Then
        If sht.Name <> "REQUEST_TABLE" Then
        If sht.Name <> "Hilfsfunktionen" Then
       For X = LBound(myArray, 1) To UBound(myArray, 1)
       Debug.Print (sht.Name)
       Debug.Print (myArray(X, fndList))
       Debug.Print (myArray(X, rplcList))
        sht.Range("A2:XFD2").Replace What:=myArray(X, fndList), Replacement:=myArray(X, rplcList), _
            LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
        Next X
        End If
        End If
        End If
        End If
      Next sht

End Sub

编辑:这基本上是在带有查找和替换值的工作表上发生的: 在第一列执行之前是查找值,然后将其覆盖。

我通过使用一个额外的 excel 文件来存储数组,然后将数据加载到 vba 中并在替换原始工作簿中的值之前再次关闭它,从而解决了这个问题。

【问题讨论】:

  • 您是否更改了工作表“tab_replace”的名称?也许工作表名称的末尾有一个空格?为避免此类错误,您可以检查工作表的代号。而且您可以适当地使用名称逻辑来帮助您进行更简单的检查,例如IF sht.Codename like "data_*" then
  • 您是说代码中没有任何变化。因此,如果我是你,我会缩短我的查找列表。将我的屏幕一分为二,同时查看 VBA 和 excel 窗口。然后逐行运行宏(使用 F8 - 跳过)并尝试在发生这种情况时进行捕捉。
  • “替换表格数组左列中的查找值”是什么意思?即使在myArray 的第一列中找不到它们是否也会被替换?
  • findlst 列中的值在.Replace 的执行过程中被覆盖,仍然不知道这是怎么发生的,但我找到了解决方法。
  • tab_replace 表格单元格值是纯文本还是公式?

标签: excel vba


【解决方案1】:

我无法重现该问题,但您可以尝试在替换之前保护工作表并在之后取消保护。这是我使用的代码。

Option Explicit

Sub Datastream_Code_Replacement()

    Const WS_NAME = "tab_replace"
    Const TBL_NAME = "tab_replace"
    
    Dim sht As Worksheet, tbl As ListObject
    Dim myArray As Variant
    Dim fndList As Integer, rplcList As Integer, X As Long

    Set tbl = Worksheets(WS_NAME).ListObjects(TBL_NAME)
    myArray = tbl.DataBodyRange
    
    fndList = 1
    rplcList = 2

    ' Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
    ' Skip the request table, so that no Reuters Codes are replaced
    Worksheets(WS_NAME).Protect
    For Each sht In ActiveWorkbook.Worksheets
    
        Select Case sht.Name
            Case WS_NAME, "CodeReplacement", "REQUEST_TABLE", "Hilfsfunktionen"
                ' do nothing
                Debug.Print "Skipped '" & sht.Name & "'"
            
            Case Else
                Debug.Print "Updating '" & sht.Name &  "'"
                For X = LBound(myArray) To UBound(myArray)
                    
                    sht.Range("A2:XFD2").Replace What:=myArray(X, fndList), _
                        Replacement:=myArray(X, rplcList), _
                        LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
                        SearchFormat:=False, ReplaceFormat:=False
                    Debug.Print X, myArray(X, fndList), myArray(X, rplcList)
                Next
                
         End Select
         
    Next sht
    Worksheets(WS_NAME).Unprotect
    
End Sub

【讨论】:

    猜你喜欢
    • 2013-07-15
    • 2014-10-12
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2018-04-11
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多