Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in vba code that takes first 4 characters from one column to another

Hello i am trying to take the first 4 characters from the B column (starting from B2) and copy them to I column (starting from I2) What is wrong in this code? It shows up as error "1004" But im not sure what is wrong in this code.

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

Maybe you can find a mistake in the code that might indicate an error, thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading