+ Reply to Thread
Results 1 to 11 of 11

Trim first word in cell

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2013
    Location
    Bratislava
    MS-Off Ver
    MS EXCEL 2021
    Posts
    90

    Trim first word in cell

    Hi gentlemen,

    I would like to ask,

    If i have column F, each row has 1 code in cell (different length), but sometimes after this code is many spaces and some other words or codes...

    How to left only first world in cell by macro ?

    thanks
    Last edited by xbr088; 06-24-2013 at 04:10 PM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Trim first word in cell

    xbr088,

    Something like this should work for you:
    myVar = Split(Trim(Range("F1").Text), " ")(0)
    MsgBox myVar
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    06-20-2013
    Location
    Bratislava
    MS-Off Ver
    MS EXCEL 2021
    Posts
    90

    Re: Trim first word in cell

    Tigeravatar,

    yes, this i need, but your formula only shows me first word in cell in popup window.

    But how to left in whole column F only first word in row's cell ?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Trim first word in cell

    What separates the code from the rest of the cell contents?

    Is it a space?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    06-20-2013
    Location
    Bratislava
    MS-Off Ver
    MS EXCEL 2021
    Posts
    90

    Re: Trim first word in cell

    yes, spaces are separating first word from other words in cell ( different amount of spaces)

    I am going to try upper solution. moment

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Trim first word in cell

    The code I provided is a sample to show you how to get what you requested. You need to apply it to your current code. I imagine you have a loop setup already that goes through column F. If you don't, here is some more example code:
    Sub tgr()
        
        Dim rngF As Range
        Dim FCell As Range
        Dim arrCodes() As Variant
        Dim CodeIndex As Long
        
        Set rngF = Range("F1", Cells(Rows.Count, "F").End(xlUp))
        ReDim arrCodes(1 To rngF.Rows.Count)
        
        For Each FCell In rngF.Cells
            CodeIndex = CodeIndex + 1
            arrCodes(CodeIndex) = Split(Trim(FCell.Text), " ")(0)
        Next FCell
        
        MsgBox Join(arrCodes, Chr(10))
        
        Set rngF = Nothing
        Set FCell = Nothing
        Erase arrCodes
        
    End Sub

  7. #7
    Registered User
    Join Date
    06-20-2013
    Location
    Bratislava
    MS-Off Ver
    MS EXCEL 2021
    Posts
    90

    Re: Trim first word in cell

    Hmm, small confusing. i tried both of your codes. and both made same. Extract first word and shows me in popup window. That's great, but after i close this popup, everything stay same, nothing is trimmed. Where i made fault ?

    Sorry for my dummy question But i need only keep first word in all row's cells in F column, and all others to delete (means others spaces, words)
    Last edited by xbr088; 06-24-2013 at 03:53 PM.

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Trim first word in cell

    Here's an alternate way to do the same thing, but it would be harder to edit and is probably not very easy to follow/understand:
    Sub tgr_v2()
        
        With Range("F1", Cells(Rows.Count, "F").End(xlUp))
            .Value = Evaluate("Index(Trim(Left(Substitute(Trim(" & .Address & "),"" "",Rept("" "",99)),99)),)")
        End With
        
    End Sub

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Trim first word in cell

    Oh, you want to replace what is in column F with the codes that get retrieved, you can do that like so:
    Sub tgr()
        
        Dim rngF As Range
        Dim FCell As Range
        Dim arrCodes() As Variant
        Dim CodeIndex As Long
        
        Set rngF = Range("F1", Cells(Rows.Count, "F").End(xlUp))
        ReDim arrCodes(1 To rngF.Rows.Count)
        
        For Each FCell In rngF.Cells
            CodeIndex = CodeIndex + 1
            arrCodes(CodeIndex) = Split(Trim(FCell.Text), " ")(0)
        Next FCell
        
        rngF.value = Application.Transpose(arrCodes)
        
        Set rngF = Nothing
        Set FCell = Nothing
        Erase arrCodes
        
    End Sub

  10. #10
    Registered User
    Join Date
    06-20-2013
    Location
    Bratislava
    MS-Off Ver
    MS EXCEL 2021
    Posts
    90

    Re: Trim first word in cell

    Both solutions works, i will use shorter. thanks

  11. #11
    Registered User
    Join Date
    06-20-2013
    Location
    Bratislava
    MS-Off Ver
    MS EXCEL 2021
    Posts
    90

    Re: Trim first word in cell

    Ou thats it :-)

    Simple question, but so difficult macro code. But anyhow is usefull. solved

    THANX

+ 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