+ Reply to Thread
Results 1 to 4 of 4

Convert a string to cell reference in VBA

  1. #1
    Registered User
    Join Date
    04-16-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    10

    Convert a string to cell reference in VBA

    I have a piece of VBA code like this

    Cell A1 has the value G4.

    varName = Range("A1").Value
    Range("C2").Formula = "='Sheet2'!" & varName

    What I'm trying to do is write this formula Sheet2!G4 within cell C2. Essentially, I guess I'm trying to achieve an indirect.

    But when i run it, C2's formula appears like this:
    'Sheet2'!'G4'

    It also shows an error. I'm assuming this is because of G4 appearing in quotes when the formula is written in the cell. How can I remove this? How can I change the string varName to a range type? Or is there any other workaround for this?


    ---------Cross-posted in Mr Excel here.---------------
    Last edited by romperstomper; 08-16-2010 at 05:43 AM. Reason: add link

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

    Re: Convert a string to cell reference in VBA

    Please use code tags for all code snippets regardless of how few lines.


    Want you have posted works as expected for me.
    Can you post workbook to illustrate the problem
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    04-16-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Convert a string to cell reference in VBA

    Hi Iswarya,

    Kindly use the code tag to specify the code part in your query.

    Can you please explain the scenario bit clearly?

    Regards,
    Ram

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Convert a string to cell reference in VBA

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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