简体   繁体   中英

Find a specific String in a range and copy it to another field in VBA

So I want to find a specific string in a range, get its address and copy its value into a new cell, which depends on the old address

Here is what I've tried until now:

Dim c As Range

For Each c In Range("F1:F1500")
    If InStr(1, c.Text, "Overall Result") Then
        Range(c).Select
        Selection.Cut
        Range(newAddress).Select
        ActiveSheet.Paste
    End If
Next c

But I'm not sure about how to get the right address of the cell. The new cell should be the old address.row and the old address.column+1

You can use c.Offset(ColumnOffset:=1) to move one column right outgoing from the range c .

Dim c As Range

For Each c In Range("F1:F1500")
    If InStr(1, c.Text, "Overall Result") Then
        c.Cut Destination:=c.Offset(ColumnOffset:=1)
    End If
Next c

Also have a look at How to avoid using Select in Excel VBA which is a bad practice, slows you down and is not very reliable.

Dim c As Range

For Each c In Range("F1:F1500")
    If InStr(1, c.Text, "Overall Result") Then
        Range(c.address).offset(0,1).value = c.text
    End If
Next c

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