Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cell VALUE into variable (Excel 2003 VBA)

    I'm trying the grab the value of a cell into a variable. Currently I'm just using the code:
    var1 = Sheets("worksheet1").Range("myRange").Value

    however causes problems if the cell contains a formula. How can I just grab the value thats being displayed?
    Thanks,

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: cell VALUE into variable (Excel 2003 VBA)

    What exactly is the problem? The code should work whether the cell contains a constant value or a formula.
    You *will* have a problem if MyRange consists of more than one cell.

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: cell VALUE into variable (Excel 2003 VBA)

    As Hans points out you will have a problem if the range is bigger than one cell. You could modify the code to avoid this
    var1 = Sheets("worksheet1").Range("myRange").Cells(1).Val ue

    Another possibility is the data type assigned to var1
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    2 Star Lounger
    Join Date
    Sep 2003
    Location
    Toronto, Ontario, Canada
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: cell VALUE into variable (Excel 2003 VBA)

    The range only contains the one cell.
    The problem is the variable will contain the formula as opposed to the value being displayed.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: cell VALUE into variable (Excel 2003 VBA)

    How is var1 declared?

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: cell VALUE into variable (Excel 2003 VBA)

    Can you post an example where this is the case?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: cell VALUE into variable (Excel 2003 VBA)

    I don't see how var1 can contain the formula when you passed it the value, unless the cell is formatted as text and contains a formula, in which case the displayed value would be the formula.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •