首页 > 解决方案 > 当单元格值为某个文本时复制和粘贴

问题描述

如果 G =“FUTURE”,我正在尝试将 H 列中的一些值复制并粘贴到 C 中。

Dim range1 As range
Dim cell As range
Set range1 = Sheets("Paste Port here - addFutureSDL").range("G:G")

For Each cell In range1
    If cell.Value = "FUTURE" Then

标签: excelvbacopypaste

解决方案


我看到您选择了整列“G”,更好的主意是设置“G”的最后一行,然后从 1 迭代到最后一行。


Sub test()

Dim sht As Worksheet
Set sht = ThisWorkbook.Sheets("Paste Port here - addFutureSDL")

Start_row = 1
Dim Last_row As Integer
Last_row = sht.Cells(sht.Rows.Count, "G").End(xlUp).Row 'finding last row (if used whole column "G" - can be deleted
Dim range1 As range
Set range1 = sht.range(Cells(Start_row, 7), Cells(Last_row, 7)) ' here u can change if u need to iterate through whole column "G" just to sht.range("G:G")

For Each cell In range1
    If cell = "FUTURE" Then
        cell.Offset(0, -4) = cell.Offset(0, 1) 'replacing
    End If
Next cell

End Sub

祝你有美好的一天 :)


推荐阅读