0

I have a sheet with Columns A to P. In columns B i have customer names. Want to find rows with substring “ABC -“ and copy the content of the cell in column B to Column G on the same row.

My code fails on this:

For I= 1 to finalrow
    If Left(Cells(I,2).Value,5) = “ABC -“ Then
Rownumber= ActiveCell.Row
Range("B" & Rownumber).Select
Range("B" & Rownumber).Copy
        Range("G" & rownumber).Select
        ActiveSheet.Paste
        Range("G" & rownumber).Select
End if

Next I
1
  • "My code fails on this" -- what do you mean exactly? What error do you get? Commented May 11, 2021 at 7:38

3 Answers 3

3

This one works as expected, writing the values from column "B" to column "G":

Sub TestMe()
    
    Dim i As Long
    For i = 1 To 10
        With ThisWorkbook.Worksheets("Sheet1")
            Dim myCell As Range
            Set myCell = .Cells(i, "B")
            If Trim(Left(myCell.Value, 5)) = "ABC -" Then
                .Cells(i, "G").Value = myCell.Value
            End If
        End With
    Next i

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it works fine. Using the . does it mean, that I refer to the reference after the With statement
@sober - you are welcome. The dot means that the line refers to the With, yes. E.g. .Cells(i, "G").Value is absolutely equal to ThisWorkbook.Worksheets("Sheet1").Cells(i, "G").Value.
1
For I = 1 To finalrow
    With Cells(I, 2)
        If .Text Like "ABC -*" Then .Offset(0, 5) = .Value
    End With
Next I

Comments

0
For I = 1 to finalrow
    If Left(Cells(I,2).Value,5) = "ABC -" Then
       Cells(I,7).Value = Cells(I,2).Value
    End if
Next I

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.