+ Reply to Thread
Results 1 to 5 of 5

Truncate cells in column IF cell begins with X

Hybrid View

  1. #1
    Registered User
    Join Date
    09-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Truncate cells in column IF cell begins with X

    Hello,

    Been a lurker for a long time, found tons of quality information here. Decided to register so I could use the local search engine. Finally making my first post.

    I have a single column with a series of text data (it's always the same column). I would like to truncate the text to 9 characters if, and only if, the cell in that column begin with X. I would like to do this through a macro.

    Help?

    Thanks in advance

    Example....
    Before:
    X123456789101112
    X567891012345
    P123456789101123
    Z123456489798130

    After:
    X12345678
    X56789101
    P123456789101123
    Z123456489798130

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Truncate cells in column IF cell begins with X

    =if(left(a1,1)="x",left(a1,9),a1)

  3. #3
    Registered User
    Join Date
    09-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Truncate cells in column IF cell begins with X

    Quote Originally Posted by JieJenn View Post
    =if(left(a1,1)="x",left(a1,9),a1)
    Thanks for the quick reply.

    That works great but how do I execute this through a Macro?

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Truncate cells in column IF cell begins with X

    This should do it

    Option Explicit
    Sub abc()
     Const WhatColumn As String = "A" '<== Change to your column
     Dim ptr As Long
     
     For ptr = 1 To Cells(Rows.Count, WhatColumn).End(xlUp).Row
        If Left(Cells(ptr, WhatColumn).Value, 1) = "X" Then
            Cells(ptr, WhatColumn).Value = Left(Cells(ptr, WhatColumn).Value, 9)
        End If
     Next
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    09-27-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Truncate cells in column IF cell begins with X

    Quote Originally Posted by mike7952 View Post
    This should do it

    Option Explicit
    Sub abc()
     Const WhatColumn As String = "A" '<== Change to your column
     Dim ptr As Long
     
     For ptr = 1 To Cells(Rows.Count, WhatColumn).End(xlUp).Row
        If Left(Cells(ptr, WhatColumn).Value, 1) = "X" Then
            Cells(ptr, WhatColumn).Value = Left(Cells(ptr, WhatColumn).Value, 9)
        End If
     Next
    End Sub
    Thank you sir, that works perfect! Greatly appreciate it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1