+ Reply to Thread
Results 1 to 6 of 6

Can't concatenate a cell address

  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Can't concatenate a cell address

    Hiya,

    This seems odd. I want to concatenate a cell address but I get an error: Run-time error '1404'; Method 'Range' of object '_Global' failed.

    here's my code:
    dim intCount as integer

    intCount = 2
    strCellValue = Range("A" + Str(intCount)).Value 'This throws the error

    strCellValue = Range("A2").Value 'This works!

    I tried changing my variable type to variant but no such luck. Any ideas?

    Cheers,
    John

  2. #2
    Registered User
    Join Date
    03-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Can't concatenate a cell address

    Ah, for some reason I had to use trim. Null terminator maybe?

    strCellValue = Range("A" + Trim(Str(intCount))).Value

    Cheers,
    J


    Quote Originally Posted by JohnSidney View Post
    Hiya,

    This seems odd. I want to concatenate a cell address but I get an error: Run-time error '1404'; Method 'Range' of object '_Global' failed.

    here's my code:
    dim intCount as integer

    intCount = 2
    strCellValue = Range("A" + Str(intCount)).Value 'This throws the error

    strCellValue = Range("A2").Value 'This works!

    I tried changing my variable type to variant but no such luck. Any ideas?

    Cheers,
    John

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437

    Re: Can't concatenate a cell address

    FYI, here is what the help says about the STR function.

    Remarks

    When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied.

    Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike Str, the Format function doesn't include a leading space for the sign of number.

    Note The Str function recognizes only the period (.) as a valid decimal separator. When different decimal separators may be used (for example, in international applications), use CStr to convert a number to a string.
    So your problem is leading space.
    Try this in the immediate window to see the difference.
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

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

    Re: Can't concatenate a cell address

    Maybe to obvious but my eye spotted:
    "A" + Str(intCount)

    Should this be "A" & Str(intCount) ?

    maybe even use CStr() for the type casting/conversion
    Last edited by rwgrietveld; 03-12-2010 at 08:30 AM.
    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
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437

    Re: Can't concatenate a cell address

    Usually the & is used for concatenation although + will work.

    Just becareful when using the + as diffirent variable types can have different results.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-10-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Can't concatenate a cell address

    Aye,

    Cheers for the info guys. Very useful indeed!

    All the best,
    J

+ 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