Search code examples
vbaexceltrim

Excel VBA script modify text trimming first 4 characters


I would like to write VBA script to trim the first characters in each row.

I know i can format cells with Cells Format > custom

But if i want to compare the content of the cells later, the trimmed characters are nevertheless taken into account in the comparison.

I am new to VBA, how can i quickly trim text for text comparison ?


Solution

  • Select the cells you want to trim and run this tiny macro:

    Sub trimmit()
        For Each r In Selection
            v = r.Text
            r.Value = Mid(v, 5)
        Next r
    End Sub
    

    I am assuming that if a cell contains ABCDE, it will become E