Search code examples
excelvba

Copy first four characters from one column to another


I am trying to copy the first four characters from column B (starting from B2) to column I (starting from I2).

This code shows

error "1004"

Dim Cell As Range
For Each Cell In Range("B2:B100").Cells
    If Cell.Value = "" Then
        Cell.Value = Left(Cell.Offset(0, -7).Value, 4)
    End If
Next
End Sub

Solution

  • You've got your columns mixed up. Loop column B, and write into column I (which is an offset of 7, not -7).

    If Cell.Value <> "" Then
       Cell.Offset(,7).Value = Left(Cell.Value, 4)
    End If