Consulting

Results 1 to 14 of 14

Thread: Writing Excel cells with VBA variables

  1. #1

    Writing Excel cells with VBA variables

    I need to store (hence display also) the result of a VBA calculation into an Excel cell.

    I cannot go through a Function and I MUST go through a variable.
    Let's call it ForDebug, which can be any type but preferably it's As Double.

    I tried this syntax, with ForDebug As Double first and As Variant later.

    [VBA] MsgBox ForDebug
    Range("K4") = ForDebug
    Range("K4").Value = ForDebug[/VBA]

    Positively, the MsgBox displays valid data (-0.59702...) but nothing happens.
    Cell K4 of the Excel sheet remains empty regardless of its format (Number or General).
    Also I've only one WorkSheet.

    What do you guys suggest?
    Last edited by MamboKing; 06-28-2008 at 11:13 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That you check that ForDebug really does have a value, because it should work.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Another thin, are you sure you are writing to the worksheet that you think that you are?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    What is the calculation? hwo are you achieveing the result? what is a standard result?, the Range("K4").Value = ForDebug would be correct, how is K4 formatted?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5

    I updated the posting...

    ...with the requested info.

    Thanks for responding!

  6. #6
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    MamboKing you didn't answer all my questions
    Quote Originally Posted by Simon
    What is the calculation? how are you achieveing the result?
    , can you post the offending workbook?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  7. #7

    Made it simpler...

    Simon,

    The calculation is really complicated. It's the output of a Finite-Difference numeric method (that I'm coding) for the calculation of a non-liner partial differential equations.

    Let's make it simpler. I wrote this simple test and behaves exactly the same. While DiplayVar contains data, the cell K4 remains empty.

    [VBA] Function DisplayCell2(Num As Variant)
    Dim DiplayVar As Variant

    DisplayVar = Num
    MsgBox DisplayVar
    Range("K4").Value = DisplayVar

    End Function[/VBA]

    I also posted the Excel & VBA here:
    http://sites.google.com/a/piac3ntini.../VBA2EXCEL.xls

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is this a UDF that you are calling from a worksheet? You said you couldn't use a function.

    If it is not a UDF, check your spelling of your variables (and the learn t9o use Option Explicit).
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    A function doesnt display a value like that (as far as i am aware) normally a function is called upon in another macro or used in the worksheet like this =displaycell2()
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  10. #10
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    xld, DiplayVar DisplayVar see nothing escapes me!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  11. #11
    Now I'm totally, totally lost!

    I corrected the mispelling and changed the Function output (re-posted at the same link).

    If I remove by commenting the line Range("K4").Value = DisplayVar

    the function outputs its value in the cell G8, where it's instantiated.

    But the line Range("K4").Value = DisplayVar, when active, screws up everything!

    It make the function not returning anything!

    That's my trouble...

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because UDFs cannot write to cells in a worksheet, they only return a result into the cell that the UDF is in.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  13. #13
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    you can pass the result back to another macro and then display a result!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  14. #14
    Quote Originally Posted by Simon Lloyd
    you can pass the result back to another macro and then display a result!
    Thanks. I think I got it to work.
    Last edited by MamboKing; 06-28-2008 at 11:41 PM.

Posting Permissions

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