【发布时间】:2021-10-28 16:24:04
【问题描述】:
我有这本包含 3 列数据的工作簿。如果 col A 中的单元格在 col E 中找到匹配值,则 col E 中的值将替换为 col B 中的值。
下面的代码给了我以下结果:
但我想实现这个:
问题是,如何让 col A 中的每个单元格循环通过 col E 的每个单元格并替换 col B 中的值在正确的位置?提前谢谢你
Sub CopyCells1()
Dim i As Integer
Dim Lastrow As Long
Dim rng As Range
Set ws = Workbooks("Control.xlsm").Worksheets("Sheet1")
Set rng = ws.Range("E1:E1" & Lastrow)
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To Lastrow
If Cells(i, 1).Value = Cells(i, 5).Value Then
Cells(i, 5).Value = Cells(i, 2).Value
Debug.Print Cells(i, 1).Value
End If
Next
End Sub
【问题讨论】:
-
您需要使用单独的内部 for 循环来步进 Col E 行。
-
@TedroyG 仅供参考 可能对展示
Application.Match()的未记录功能的后期帖子感兴趣。 -
@T.M.我会看看你的帖子。谢谢!
标签: excel vba loops replace copy