+ Reply to Thread
Results 1 to 4 of 4

String concatenation

  1. #1
    shishi
    Guest

    String concatenation

    Hi,

    Is there a better way to concatenate strings in excel vba.
    The below code is not giving me the desired results. Could you please
    tell what could be the reason?

    For i = 1 To nmodules

    Sname = "START_" & Str(i)
    MsgBox "The String is " & Sname
    Next i

    The result is
    START_<space>1
    START_<space>2 .....and so on

    Excel is placing a space between the arguments. The desired results is
    START_1, START_2 etc.

    shi


  2. #2
    Chip Pearson
    Guest

    Re: String concatenation

    Use

    Sname = Sname & "START_" & Str(i) & ", "


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "shishi" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > Is there a better way to concatenate strings in excel vba.
    > The below code is not giving me the desired results. Could you
    > please
    > tell what could be the reason?
    >
    > For i = 1 To nmodules
    >
    > Sname = "START_" & Str(i)
    > MsgBox "The String is " & Sname
    > Next i
    >
    > The result is
    > START_<space>1
    > START_<space>2 .....and so on
    >
    > Excel is placing a space between the arguments. The desired
    > results is
    > START_1, START_2 etc.
    >
    > shi
    >




  3. #3
    shishi
    Guest

    Re: String concatenation

    Hi Mr. Pearson,

    I tried using the above solution, still the excel is placing a space
    between START_ and 1. So I get results like START_ 1. Any more ideas??

    Thanks,
    Shi


  4. #4
    Jim Cone
    Guest

    Re: String concatenation

    Shi,

    I think Chip may have gone to dinner...

    The "Str" function automatically adds a space to contain the sign of the number.
    The "Cstr" function would work for you, however it is not required.
    The "&" operator will convert the i variable to a string so,
    assuming that Sname is a String...

    Sname = "START_" & i

    is all you need.

    Jim Cone
    San Francisco, USA



    "shishi" <[email protected]> wrote in message news:[email protected]...
    Hi,
    Is there a better way to concatenate strings in excel vba.
    The below code is not giving me the desired results. Could you please
    tell what could be the reason?
    For i = 1 To nmodules
    Sname = "START_" & Str(i)
    MsgBox "The String is " & Sname
    Next i
    The result is
    START_<space>1
    START_<space>2 .....and so on
    Excel is placing a space between the arguments. The desired results is
    START_1, START_2 etc.
    shi

+ 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