+ Reply to Thread
Results 1 to 19 of 19

Assign value to a name in VBA

  1. #1
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Lightbulb Assign value to a name in VBA

    Dear All,
    I used below to assign a value to a name in VBA but, VBA said there is an error 1004 as" application defined or object defined error".

    Please Login or Register  to view this content.
    Any suggestions? Where can I find some document about using Names in VBA?

    Thanks.

    Regards,
    SpringLily
    Last edited by SpringLily; 09-22-2010 at 04:54 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Assign value to a name in VBA

    What is MyValue ?

    Check this out
    http://www.cpearson.com/excel/DefinedNames.aspx
    Last edited by davesexcel; 09-21-2010 at 05:02 AM.

  3. #3
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    myValue is defined as double and is predefined value. see below

    Please Login or Register  to view this content.

    Thanks.

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

    Re: Assign value to a name in VBA

    Macro recorder would have helped point you in the right direction

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    Sorry, Andy. Myvalue is not a predefined name. It is a variable defined in the VBA sub(). I tried the formula. it seems do not work. Thanks anyway. Any other suggestions?

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: Assign value to a name in VBA

    Try this,
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    hi, davesexcel
    The name Rvalue has been defined already. Any other suggestions?

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Assign value to a name in VBA

    Consider this code as an example:

    Please Login or Register  to view this content.



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

    Re: Assign value to a name in VBA

    Not sure what the issue is when you are trying it but for me this code sets the existing named range Rvalue to the value contained in the vba variable MyValue.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    Hi,there
    I tried three methods as below:
    Please Login or Register  to view this content.
    It seems only A works and it changes the value in the cell where Rvalue referes to. The other two change the defination of the "Rvalue" to 0.35. Any suggestions to improve the code?
    Thank.

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

    Re: Assign value to a name in VBA

    I misunderstood, I thought your named range was a constant.

    Assuming the named range refers to a cell such as A1 then this shoud update the contents of A1

    Please Login or Register  to view this content.
    If you get the 1004 error it would strongly suggest that thisworkbook does not contain a named range RValue.

  12. #12
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    Thanks a lot, Andy. It works. Can you predict any problems if I use this line in a function? I used in a function and whenever code goes to this line, it finishes without runing rest of the code and of couse, gives wrong value.

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

    Re: Assign value to a name in VBA

    You don't say but I will guess you are trying to use the function from a worksheet cell. In which can you can not alter the contents of other cells.

  14. #14
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    You are right. Here is the problem. I have a lookup table built in Excel sheet and the value in the table changes with a parameter calculated from upstream calculations. Additionally, this talbe will be used for different cases in the same sheet and the parameter for different cases are different. Any sugguestion the best way to solve it? Thanks a lot. You are a star!

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

    Re: Assign value to a name in VBA

    You need to post a workbook example of your data layout and formula.

    Then you need to explain in detail the input and expected outputs.

  16. #16
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    Please see attached for the spreadsheet. Thanks.
    Attached Files Attached Files

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

    Re: Assign value to a name in VBA

    An explanation would have been nice, save me having to back audit your formula and code.

    The usual way around setting values via worksheet formula is to use the worksheet change event.

    But you will have problems as you have multiple formula all wanting to change the contents is CreditTables!Q1. Once you change it for 1 formula you cause the other formula to recalculate.

    What you probably need is a table of calculations for fixed Rvalues.

  18. #18
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    Based on type, different table is chosen for VLOOKUP. For type 3, Rvalue will be used to generate the look up table. Thanks.

  19. #19
    Registered User
    Join Date
    09-13-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    88

    Re: Assign value to a name in VBA

    Hi, there
    I solved the problem by creating a array which contains the third table in VBA. Thanks everyone for your contribution. Regards, SpringLily

+ 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