+ Reply to Thread
Results 1 to 13 of 13

Mixing Excel Formulas/Functions with VBA

  1. #1
    Registered User
    Join Date
    03-23-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    5

    Mixing Excel Formulas/Functions with VBA

    I have a final number that is derived from many cells in excel generated firstly from =Rand(). What I want to do is add that number to the total of all previous running totals and so on. However I have run into a snag every time i try and make the Target.Address = "that cell" it does nothing. What am I doing wrong here can anyone help?

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Mixing Excel Formulas/Functions with VBA

    Hi

    We need to see the code in the context of its workbook
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Mixing Excel Formulas/Functions with VBA

    If you are using Target.Address then VBA will be trying to set the Address of the range Target, Address is a read only property of Range so you can't change it.

    To put a value in the range Target use Target.Value.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    03-23-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Mixing Excel Formulas/Functions with VBA

    Here is the Code
    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Mixing Excel Formulas/Functions with VBA

    That code should work, are there any errors, unexpected results?

  6. #6
    Registered User
    Join Date
    03-23-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Mixing Excel Formulas/Functions with VBA

    I can link the number from one cell to C5 without error but the total of C5 does not add to C6 except for the first time I activate the code or put a number directly into C5.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Mixing Excel Formulas/Functions with VBA

    That code will only add to C6 when you manually enter something in C5.

    What is it you want to do?

    Is C5 a formula?

    What sort of 'link' are you referring to?

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Mixing Excel Formulas/Functions with VBA

    Hi,

    That code works for me. Whether it's doing what you want it to do is a moot point.

    i.e. starting with C5 and D5 empty, successively entering 1,2,3,4.. etc in C5 results in 1,3,6,10...etc. in D5

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Mixing Excel Formulas/Functions with VBA

    Oops, meant D5 not C6.:oops:

  10. #10
    Registered User
    Join Date
    03-23-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Mixing Excel Formulas/Functions with VBA

    C5 is the end result of a formula yes. Formula in excel is;
    K2=RAND()<=.23 K2=IF(K2=TRUE,RAND()) M2=L2*2000*RAND() N2=RAND() O2=M2*N2
    sorry dont know how to add excel cells in the forum just yet. This is being used for a random generator for pounds of gravel of certain types estimated per day and the amount of usable material within said gravel.

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Mixing Excel Formulas/Functions with VBA

    Hi,

    I don't understand. You seem to be implying that C5 gets its value from K2. But the formula you show for (presumably K2) is not understandable and in any case seems to be self referencing and therefore circular. Not sure where M2:O2 enter the debate.

    Upload the workbook (the FAQ tells you how) and add some notes which clearly describe what you want to do, and manually add add some typical results so that we can see your goal.

  12. #12
    Registered User
    Join Date
    03-23-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Mixing Excel Formulas/Functions with VBA

    The formula can be interchanged so the only problem I am having is using a formula say =Rand() as C5 in the VB code simply put. how can I get VB to work with a randomly generated number without manually inputting it?

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Mixing Excel Formulas/Functions with VBA

    Hi,

    Perhaps by using the VBA Rnd syntax?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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