将特定单元格数据移动到另一个单元格

时间:2019-03-07 03:02:45

标签: excel vba

我正在尝试将特定数据“ 1”和“ 2”从一个单元格移动到另一个单元格,并且代码出现问题。单元格数据移动,但是我一直在获取“ false”而不是数据移动。有人可以帮我吗?谢谢!

enter image description here

Sub tested()
    Dim rng As Range
    Dim lrow As Integer, irow As Integer

    With ActiveSheet
        lrow = .Range("A" & Rows.Count).End(xlUp).row

        For Each rng In .Range("A1:A" & lrow)
            If InStr(rng.Value, "1") > 0 Then
                rng.Offset(0, 1).Value = rng.Value = rng.Value = ""
            End If
        Next rng

        irow = .Range("A" & Rows.Count).End(xlUp).row

        For Each rng In .Range("A1:A" & lrow)
            If InStr(rng.Value, "2") > 0 Then
                rng.Offset(0, 2).Value = rng.Value = rng.Value = ""
            End If
        Next rng
    End With

1 个答案:

答案 0 :(得分:0)

调整具有此位置的行:

rng.Offset(0, 1).Value = rng.Value = rng.Value = ""

对此

rng.Offset(0, 1).Value = rng.Value

新代码:

Sub tested()

    Dim rng As Range
    Dim lrow As Integer, irow As Integer

    With ActiveSheet

        lrow = .Range("A" & Rows.Count).End(xlUp).Row

        For Each rng In .Range("A1:A" & lrow)

            If InStr(rng.Value, "1") > 0 Then

                rng.Offset(0, 1).Value = rng.Value
                rng.ClearContents

            ElseIf InStr(rng.Value, "2") > 0 Then

                rng.Offset(0, 2).Value = rng.Value
                rng.ClearContents

            End If

        Next rng

    End With

End Sub