简体   繁体   中英

Copy certain values from one to another column and deleting the original value

I want to copy values from one column to another column (into the same row) if the cell contains the word IN and delete the original value. If not, the code should proceed to the next row and perform a new test. Thus the cell in the target column will remain empty.

When I run the code in Excel nothing happens, so I don't know what is wrong.

Ideally the code should jump to the next column (8) and do the same search and paste the value into the same column (5) when it is done with the first column, but this I haven't started with yet. So I do appreciate tips for that as well :)

Sub Size()
    Dim i As Integer, a As String
    i = 2
    a = "IN"

    Do While Cells(i, 7).Value <> ""
        If InStr(Cells(i, 7), a) Then
            'copying the value to another column but within the same row
            Cells(i, 7).Copy Cells(i, 5) 
            Cells(i, 7).Clear
            i = i + 1
        Else
            i = i + 1
        End If
    Loop
End Sub

I found out that my first cell in column 7 was empty and thus the Do While Cells(i, 7).Value <> "" wasn't working. Hence I'm refering to a different column that always contain data. Note that the solution code also jumps to the 2 next columns in order to search for the same word.

Sub Size()
Dim i As Integer, a As String
j = 0
i = 1
a = "IN"
Range("A1").Offset(i, 0).Select

For j = 0 To 2
    Do Until Selection.Value = ""
        If InStr(Range("G1").Offset(i, j).Value, a) Then
        Range("E1").Offset(i, 0).Value = Range("G1").Offset(i, j).Value
        Range("G1").Offset(i, j).Clear
        i = i + 1
        Range("A1").Offset(i, 0).Select
        Else
        i = i + 1
        Range("A1").Offset(i, 0).Select
        End If
    Loop
    i = 1
    Range("A1").Offset(i, 0).Select
Next j
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM