+ Reply to Thread
Results 1 to 19 of 19

Assign value to a name in VBA

Hybrid View

  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".

    ThisWorkbook.Names("Rvalue").refertorange.Value = MyValue
    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

    myValue=0.35

    Thanks.

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

    Re: Assign value to a name in VBA

    Macro recorder would have helped point you in the right direction

        Thisworkbook.Names("RValue").RefersToR1C1 = "=" & MyValue
    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,
        MyValue = 0.35
    
        ActiveWorkbook.Names.Add Name:="Rvalue", RefersToR1C1:=MyValue

  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:

    Sub snb()
      newvalue = 3.5
      Range("A1:C10").Name = "newname_snb"
      Range("newname_snb").Value = newvalue
    End Sub



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

    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.

    Sub Macro1()
    '
    ' Macro1 Macro
    '
        Dim MyValue As Double
        
        MyValue = 0.35
        ThisWorkbook.Names("RValue").RefersToR1C1 = "=" & MyValue
        
    End Sub

  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:
    A:  Range("Rvalue") = myRvalue
    B:  ThisWorkbook.Names("Rvalue").Value = myRvalue
    C:  ThisWorkbook.Names("Rvalue").RefersToR1C1 = "=" & myRvalue
    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
    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