+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: maintain only first word in a cell

  1. #1
    Registered User
    Join Date
    03-02-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2003
    Posts
    6

    Unhappy maintain only first word in a cell

    Everyone,

    I am having a problem with achieving the following:

    I have several rows in a column, wheras in each cell the conent is something like this

    testing corp road 25, NY NY
    TESTING inc road 35 DC
    TESting ltd road 25

    I now want to take the first word in the first row and past it in that specific first row and every other row. Hence, end result would be

    testing
    testing
    testing

    I would also be fine with just keeping the first word in every row, if you consider that making more sense (thus, I would skip the pasting into the first row and every other row). In that case it would look something like this

    testing
    TESTING
    TESting

    It would not look as nice, but I could life with that.

    Please note that the number of rows can vary (anything between 50 to 500). But I am confident that I can get that done myself (without selecting the cells. I will try to make a loop).

    As always, I would be more than grateful for any suggestions how to program a macro for this in VB.

    thank you in advance.
    regards,
    tony
    Last edited by patentprio; 03-10-2010 at 01:40 PM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    6,202

    Re: maintain only first word in a cell

    Here, try this:

    =LOWER(IF(ISERROR(FIND(" ";A1)-1);A1;LEFT(A1;FIND(" ";A1)-1)))
    Attached Files Attached Files
    Last edited by zbor; 03-07-2010 at 06:47 AM.
    "Relax. What is mind? No matter. What is matter? Never mind!"

  3. #3
    Valued Forum Contributor
    Join Date
    10-14-2006
    Posts
    321

    Re: maintain only first word in a cell

    =lower(left(a1,find(" ",a1&" ")-1))

  4. #4
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: maintain only first word in a cell

    Although the two answers are OK they do not answer to the requirement
    I now want to take the first word in the first row and past it in that specific first row and every other row.
    This is circular references and if you want to keep the calculation mode in Automatic, you must go to VBA., which is mentioned by patentprio twice.

    My question is:
    Please note that the number of rows can vary (anything between 50 to 500)
    Should all of these be replaced with the first word in the first cell of these 50-500? Are there blank cells in between? What happens to those?
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  5. #5
    Registered User
    Join Date
    03-02-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: maintain only first word in a cell

    thank you for your comment rwgrietveld. You are correct. I really need to do this in VBA.

    To answer your questions:

    yes, all of them should be replaced with the first word in the first cell (i.e. all, for example, 100)

    No, there are no blank cells in between.

    thank you.

    best regards,
    tony

  6. #6
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: maintain only first word in a cell

    The following program does that.

    Select al the cells (type A1:A500 in the namebox) and run the code
    Sub replace1()
    
    Dim SplitArray
    
    With Selection
      SplitArray = Split(.Cells(1, 1).Value, " ")
      .Value = SplitArray(0)
    End With
      
    End Sub
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  7. #7
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    6,202

    Re: maintain only first word in a cell

    Here is attempt of a solution wich doesn't working...

    I hope DonkeyOte can help since my version is adapted his (working, of course) solution of this:

    Converting words to UPPER letters:

    Public Sub ConvertUpper()
    With Range("A1:A1000")
        .Value = Evaluate("IF(ISTEXT(" & .Address & "),UPPER(" & .Address & "),REPT(" & .Address & ",1))")
    End With
    End Sub
    Now.. I've tried just to adopt it for extracting first word and make it LOWER:

    Public Sub FirstWord2()
    With Range("A1:A1000")
        .Value = Evaluate("IF(ISERROR(FIND("" ""," & .Address & ")-1),LOWER(" & .Address & "),LOWER(LEFT(" & .Address & ",FIND("" ""," & .Address & ")-1))")
        
    End With
    End Sub
    Howeve, it doesn't working...
    JBeaucaire aso tried to help me with this solution, but can't get it work either:

    Public Sub FirstWord()
    Dim RNG As Range
    Set RNG = Range("A:A").SpecialCells(xlCellTypeConstants)
    Debug.Print RNG.Address
    
        With RNG
            .Value = Evaluate("IF(ROW(1:" & .Address & "),LOWER(IF(ISERROR(FIND("" ""," & _
                .Address & ")-1)," & .Address & ",LEFT(" & .Address & ",FIND("" ""," & .Address & ")-1)))")
        End With
    
    End Sub
    DonkeyOte, your move for teaching lesson
    "Relax. What is mind? No matter. What is matter? Never mind!"

  8. #8
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: maintain only first word in a cell

    Zbor,

    I do not understand your attempt. Have you tested my code. Where is it not fullfilling the OP's requirements? ... and why the UPPER/LOWER. Don't see that in the OP's request.

    Take the first word in the first row and place that word in all of the other rows.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  9. #9
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    6,202

    Re: maintain only first word in a cell

    I have no doubt in your solution.
    I just can't figure why this doesn't work (That's the question in my post).

    And about LOWER (not UPPER; that was solution somewhere else) I get from this:

    Hence, end result would be

    testing
    testing
    testing

    ...

    testing
    TESTING
    TESting

    It would not look as nice, but I could life with that.
    Althought, I don't need to be right.
    "Relax. What is mind? No matter. What is matter? Never mind!"

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,223

    Re: maintain only first word in a cell

    Ricardo, your loop solution is most likely just fine, I didn't test it because I trusted it worked.

    The EVALUATE() trick is one we're still trying to get adept at applying, it allows an entire range of values to be processed all at once pretty much instantaneously. It's an awesome technique. But the syntax for coercing Excel to do it is nothing short of an Olympic hurdle.

    I've been fiddling with it for a couple of months and have successfully applied it to about 3 of the 10 things I've tried. In one that it worked, it was doing 50,000 value changes in less than 1 second, without turning off screenupdating.

    So, this is worth the effort. So far, Don is the only one here that seems to have mastered it, I keep coming up short on the application.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  11. #11
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: maintain only first word in a cell

    Sorry for not understanding the underlying reason. It is worth the challenge for efficiency reasons.

    Evaluation is not my specialty, but is this what you need:
    Sub Eval()
    
    With Range("A1:A13")
      .Value = Evaluate("Left(" & .Address & ", Find("" "", " & .Address & ", 1) - 1)")
    End With
    
    End Sub
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  12. #12
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    6,202

    Re: maintain only first word in a cell

    Still doesn't work for me...

    From:

    fdsfd fsfsd
    fds f dsf
    gfds r rgd
    rg
    44t4
    ferfer ger
    43
    fref rfe r
    tef 
      rfd
    4t4 4
    t 4t4 
    34rt43  4
    It gives me:

    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    fdsfd
    Same as both (mine and JB) solutions in post #7
    Last edited by zbor; 03-08-2010 at 08:38 AM.
    "Relax. What is mind? No matter. What is matter? Never mind!"

  13. #13
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: maintain only first word in a cell

    Guys - interesting as this is (evaluate) the use of Evaluate in terms of zbor's example has little / no bearing on OP's question (at least not as I see it).

    I interpret OP's request as "applying first word from first cell to all subsequent cells"

    Ricardo solved this without iteration and I see no value in using Evaluate here (at all) - discussing it in the context of OP question doesn't really add any value and may confuse.

    If there are questions pertaining to use of Evaluate (ie generating variable results without iteration) why not kick off your own thread ?

    EDIT:

    And FWIW Ricardo has also solved OP request using Evaluate

  14. #14
    Registered User
    Join Date
    03-02-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: maintain only first word in a cell

    thank you all for your comments.

    rwgrietveld, your solution :

    "
    Sub Eval()

    With Range("A1:A13")
    .Value = Evaluate("Left(" & .Address & ", Find("" "", " & .Address & ", 1) - 1)")
    End With

    End Sub
    "

    really does almost everything I need. I have tried to make it work with a variable range ( I thought I would be able to achieve that, but I didn't).

    So, is it possible to do it with a variable row range (see, some files have 100 other 145 rows or the like).

    If I set the Range in your code to a big enough number, it would copy the first word in every row up to the defined row number. However, I only want it to copy it in a row in which there is a text and stop after the last row with a text in it.

    How can I achieve that?

    Thank you in advance.
    tony

  15. #15
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: maintain only first word in a cell

    Then I would use this (I stepped away from the evaluate)

    Sub Eval()
    
    Dim MyWs As Worksheet
    Dim MyRng As Range, Ccell As Range
    Dim SplitArray
    
    Set MyWs = Worksheets("Sheet1")
    
    With MyWs
      Set MyRng = .Range(.Cells(1, 1), .Cells(.Cells.Rows.Count, 1).End(xlUp))
    End With
    
    SplitArray = Split(MyRng.Cells(1, 1).Value, " ")
    
    For Each Ccell In MyRng
      If Not IsEmpty(Ccell) Then Ccell = SplitArray(0)
    Next
    
    End Sub
    Note that MyRng starts with Cells(1,1) which is cell A1 !
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

+ 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.2.0