Excel VBA – search,抵消,replace

我有一个设置表,D列中有唯一标识符,F列中有replace值。我需要:

  • 循环浏览图纸settings栏D中的所有序列号
  • 在工作表test列A中find具有相同序列的行
  • settings列F获取replace值
  • replacetest表中与列表B中的数据相同的行

听起来很简单,但是当用下面的代码定义forto语句时,我得到了一个types不匹配的错误。

 Sub Replace_List() Dim rList As Range, cell As Range, n As Long Application.ScreenUpdating = False With ThisWorkbook.Sheets("Settings") Set rList = .Range("D4", .Range("D" & Rows.Count).End(xlUp)) End With For Each cell In rList For n = cell.Value To cell.Offset(0, 2).Value Step 1 ThisWorkbook.Sheets("test").Columns("B:B").Replace What:=n, _ Replacement:=cell.Offset(0, 2).Value, _ LookAt:=xlWhole Next n Next cell Application.ScreenUpdating = True MsgBox "Replaced all items from the list.", vbInformation, "Replacements Complete" End Sub 

任何指针,我在做什么错误在这里赞赏。 谢谢,A2k

编辑截图如下:

设置 – 我正在查找调查ID,并想用正确的datereplace原始date 在这里输入图像说明

我相信你想用Find来查找每个事件,然后用find的位置的Offsetreplace这个值:

 Sub Replace_List() Dim rList As Range, cel As Range, n As Long Dim fnd As Range Dim fndFirst As String Application.ScreenUpdating = False With ThisWorkbook.Sheets("Settings") Set rList = .Range("D4", .Range("D" & .Rows.Count).End(xlUp)) End With For Each cel In rList Set fnd = ThisWorkbook.Worksheets("test").Columns("A:A").Find(What:=cel.Value, LookAt:=xlWhole) If Not fnd Is Nothing Then fndFirst = fnd.Address Do fnd.Offset(0, 1).Value = cel.Offset(0, 2).Value Set fnd = ThisWorkbook.Worksheets("test").Columns("A:A").FindNext(After:=fnd) Loop While fnd.Address <> fndFirst End If Next Application.ScreenUpdating = True MsgBox "Replaced all items from the list.", vbInformation, "Replacements Complete" End Sub