+ Reply to Thread
Results 1 to 18 of 18

How to Add a String to the beginning of every value in a range

  1. #1
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    How to Add a String to the beginning of every value in a range

    Hello Excel Forum!

    I could use some help on an easy one. My goal is to identify all cells within a column that have values, then add the string "E-" to the beginning of each cell.

    I know how to identify the the cells with values within my range and I know that I need to use & to concatenate my string with each cell.

    I am not sure how to identify each value within the loop and then how to actually execute the concatenate.

    Here is my first attempt:
    Please Login or Register  to view this content.
    Any help would be greatly appreciated!

    -tvxl

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to Add a String to the beginning of every value in a range

    Maybe:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

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

    Re: How to Add a String to the beginning of every value in a range

    Try this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    Re: How to Add a String to the beginning of every value in a range

    Thanks for your response!

    I tried the following and got the error "Object variable or With block variable not set" on the line
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to Add a String to the beginning of every value in a range

    Hi,

    You need a Set statement with object variables like Ranges
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    Re: How to Add a String to the beginning of every value in a range

    Quote Originally Posted by xlnitwit View Post
    Hi,

    You need a Set statement with object variables like Ranges
    Please Login or Register  to view this content.
    Yes, Thank you!!

    Quick question: why didn't we have to tell the sub what invoiceNameValue was? We just told it that we would change all invoiceNameValue's within the range, but we never specified what it was (beyond indicating it was a variant above). I don't quite understand how it know what were talking about.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to Add a String to the beginning of every value in a range

    Strictly speaking it would have been better to declare it as a Range. The code is equivalent to
    Please Login or Register  to view this content.
    so it just loops through each cell in the specified range and amends it as required.

  8. #8
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    Re: How to Add a String to the beginning of every value in a range

    Quote Originally Posted by xlnitwit View Post
    Strictly speaking it would have been better to declare it as a Range. The code is equivalent to
    Please Login or Register  to view this content.
    so it just loops through each cell in the specified range and amends it as required.
    Didn't we declare it as a range at the top when we said:

    Please Login or Register  to view this content.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to Add a String to the beginning of every value in a range

    I mean that invoiceNameValue should be declared as a Range too.

  10. #10
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    Re: How to Add a String to the beginning of every value in a range

    I put the contents of this sub within the contents of a much larger sub and the line below gives me the error "Application-defined or object-defined error":

    Please Login or Register  to view this content.
    Any idea what that means?

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to Add a String to the beginning of every value in a range

    Try this instead:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    Re: How to Add a String to the beginning of every value in a range

    That gives me a compile error of "Expected: list separator or )"

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: How to Add a String to the beginning of every value in a range


    Hi !

    Another way :

    PHP Code: 
    Sub Demo1()
        
    With Sheet1.Range("B2"Sheet1.Cells(Rows.Count2).End(xlUp))
            .
    Value Evaluate(Replace("IF(#>"""",""E-""&#,"""")""#", .Address))
        
    End With
    End Sub 
    As Sheet1 is a worksheet CodeName, safer than a worksheet name if renamed …

    2016-02-22_195835.png

    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 03-05-2018 at 02:21 PM.

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to Add a String to the beginning of every value in a range

    This works:

    Please Login or Register  to view this content.

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: How to Add a String to the beginning of every value in a range


    It may not work if the active worksheet is not "Sage Import" 'cause of your inner Range("B2") without any worksheet before !

    The reason why I prefer to use a CodeName like in my previous post …
    Last edited by Marc L; 03-05-2018 at 02:26 PM.

  16. #16
    Registered User
    Join Date
    01-17-2018
    Location
    Buffalo, New York
    MS-Off Ver
    2016
    Posts
    24

    Re: How to Add a String to the beginning of every value in a range

    Quote Originally Posted by xladept View Post
    This works:

    Please Login or Register  to view this content.
    Oddly enough it works within it's own sub. However, when I next it within the rest of my sub, it halts at row 19 on every page that I try it with. Any idea why that might be?

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

    Re: How to Add a String to the beginning of every value in a range

    Try this.
    Please Login or Register  to view this content.

  18. #18
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How to Add a String to the beginning of every value in a range

    The xlDown can be hinky that way - the last row in B is for the sheet in play but if the sheet in play is not the active sheet then it can vary wildly - better to use Norie's solution, which is the way I would have answered:

    Please Login or Register  to view this content.
    This code, like Norie's tells the program exactly where to get the row count

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Add a string between beginning and and of a chain per corresponding code
    By percuk17 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-16-2017, 12:25 PM
  2. Replies: 5
    Last Post: 10-25-2014, 12:35 AM
  3. Move Text From End Of String To Beginning
    By Tellm in forum Excel General
    Replies: 4
    Last Post: 06-28-2013, 06:09 AM
  4. Replies: 5
    Last Post: 09-08-2010, 03:45 PM
  5. Replies: 3
    Last Post: 09-08-2010, 09:49 AM
  6. Add a string at the beginning and end
    By patyandra in forum Excel General
    Replies: 2
    Last Post: 12-05-2009, 07:05 PM
  7. Is there away to add a zero to the beginning of number string?
    By Anthony in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-15-2006, 06:55 PM

Tags for this Thread

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