+ Reply to Thread
Results 1 to 7 of 7

Text Manipulation

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2010
    Location
    hillsboro, oregon
    MS-Off Ver
    Excel 2007
    Posts
    4

    Text Manipulation

    I have a spreadsheet with about 5000 entries, and I'm trying to automate the updates for certain pieces of information.

    I have two columns of text, representing a range of years, i.e., 2005-2009 and a second column representing the component years of that range, i.e., 2005, 2006, 2007, 2008, 2009. Another example is 2010-PRESENT (where PRESENT is entered annually, i.e., 2011). Below is what the data looks like (except no cell boxes):

    2005-2009 2005, 2006, 2007, 2008, 2009
    2005-2009 2005, 2006, 2007, 2008, 2009
    2005-2009 2005, 2006, 2007, 2008, 2009
    2010-PRESENT 2010, 2011
    2010-PRESENT 2010, 2011
    2010-PRESENT 2010, 2011

    This information has all been entered manually.

    I've been able to develop a logic test that handles reading to see if PRESENT is part of the date, and updating the component years accordingly. It is: =IF(RIGHT(D21,1)="T", CONCATENATE(D21,4),", ",CURRENT_YEAR))). CURRENT_YEAR is a named cell and assigned the value of 2011 for this model year.

    For data which doesn't include PRESENT, it fails the logic test and returns a "FALSE" instead of leaving the existing data. I have not been able to develop anything that will keep/update the component years if I run the other formula on it.

    Ideally, I would have a macro or something to read the first and ending years, then fill the cells in the second column with the component years.

    I would appreciate your thoughts and ideas, so I can get out of manually doing these updates every year.

    Thanks!

    Al

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Text Manipulation Questions

    If we assume you strings are in A1:A6 then

    B1:
    =IF(MAX($A1:A1)=RIGHT(SUBSTITUTE($A1,"PRESENT",CURRENY_YEAR),4)+0,"",LEFT($A1,4)+COLUMNS($B1:B1)-1)
    copied across matrix  B1:F6 (or however far across you deem necessary)
    There's a fair amount of repetition going on in the above though fairly lightweight...

  3. #3
    Registered User
    Join Date
    09-08-2010
    Location
    hillsboro, oregon
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Text Manipulation Questions

    Sorry, the screen editor played havoc with the layout of the data. Sorry for the confusion, it should look like: (Ignore the ...'s as they are there only to force the correct look of the two columns of data)

    ______COL A__________________COL B_________
    ROW 1|2005-2009..................|.2005, 2006, 2007, 2008, 2009
    ROW 2|2010-PRESENT..........|.2010, 2011

    Thank you again for considering my problem!

    Al
    Last edited by shg; 09-09-2010 at 02:30 PM. Reason: deleted quote

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Text Manipulation Questions

    If you want a delimited single cell string then I would opt for a UDF

    Function SplitYears(rngY As String, Optional strDelim As String = "-") As String
        Dim vYrs As Variant
        On Error GoTo Handler:
        vYrs = Split(rngY, strDelim)
        If Not IsNumeric(vYrs(1)) Then vYrs(1) = Range("CURRENT_YEAR")
        vYrs = Evaluate("TRANSPOSE(ROW(" & vYrs(0) & ":" & vYrs(1) & "))")
        SplitYears = Join(vYrs, ",")
        Exit Function
    Handler:
        SplitYears = "Invalid"
    End Function
    The above, stored in a module in VBE in a Macro Enabled file can be used along the lines of:

    B1: =SPLITYEARS(A1)
    copied down

  5. #5
    Registered User
    Join Date
    09-08-2010
    Location
    hillsboro, oregon
    MS-Off Ver
    Excel 2007
    Posts
    4

    Thumbs up Re: Text Manipulation Questions

    That did the vast majority of what I needed!! The only thing it stumbled on was single year items, i.e., 2007, but there weren't many of those and I was able to find them.

    Sorry - can't seem to find how to indicate this is solved, but thank you very much!

    Al

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Text Manipulation Questions

    Sorry I should have thought of that.

    Also, in retrospect it would be much (much) better to pass CURRENT_YEAR named range as an argument in the UDF call so as to make it a direct precedent
    (altering CURRENT_YEAR would in turn recalculate all cells using the UDF without need for Volatility)

    Function SplitYears(rngY As String, rngCY As Range, Optional strDelim As String = "-") As String
        Dim vYrs As Variant
        On Error GoTo Handler:
        vYrs = Split(rngY, strDelim)
        If UBound(vYrs) = 0 Then
            SplitYears = vYrs(0)
        Else
            If Not IsNumeric(vYrs(1)) Then vYrs(1) = rngCY(1)
            vYrs = Evaluate("TRANSPOSE(ROW(" & vYrs(0) & ":" & vYrs(1) & "))")
            SplitYears = Join(vYrs, ",")
        End If
        Exit Function
    Handler:
        SplitYears = "Invalid"
    End Function
    Now called with additional parameter, eg:

    =SPLITYEARS(A1,CURRENT_YEAR)

  7. #7
    Registered User
    Join Date
    09-08-2010
    Location
    hillsboro, oregon
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Text Manipulation Questions

    Thanks again for your generous support! The latest change will make this something I can continue to use in the future.

    Cheers!

    Al

+ 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