+ Reply to Thread
Results 1 to 9 of 9

Question: how to use variables in formulas?

  1. #1
    Registered User
    Join Date
    08-09-2014
    Location
    Finland
    MS-Off Ver
    2013
    Posts
    4

    Question: how to use variables in formulas?

    Hey,

    I'm stuck with this one, please help. I'm trying to use variable in formula but excel gives Run-time error 1004: Application-defined or object-defined error.

    Original and working line in the code was:

    Cell.Offset(0, 3).FormulaR1C1 = "=(1-COS((6.28318*RC[-3])/102.3))/2"

    Then I tried to replace value 102.3 with variable MaxTime (value defined earlier in code):

    Cell.Offset(0, 3).FormulaR1C1 = "=(1-COS((6.28318*RC[-3])/" & MaxTime & "))/2"

    Any idea why this doesn't work? Also following line works without problems:

    Cell.Offset(0, 3).FormulaR1C1 = "=(1-COS((6.28318*RC[-3])/" & "102.3" & "))/2"

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Question: how to use variables in formulas?

    What's in MaxTime? In Excel and, therefore, VBA, a time is represented by a numeric value which is a fraction of a 24 hour period (a day). So, 0.25 is 06:00, 0.5 is 12:00 PM, 0.75 is 18:00, etc. MaxTime sort of implies you have a time value in there so perhaps it isn't equivalent to 102.3


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-09-2014
    Location
    Finland
    MS-Off Ver
    2013
    Posts
    4

    Re: Question: how to use variables in formulas?

    Thank you for your reply. I have data set where I have time variable, and MaxTime is the highest value in the data set. I used msgbox to confirm that MaxTime has value 102,3.

    I began to doubt that decimal separator "," has something to do with error. Indeed, posted line works well when MaxTime has integer value. After this I changed default decimal separator from options of Excel from "," to ".". Unfortunately, msgbox still says that value of MaxTime is 102,3 and posted line doesn't work.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Question: how to use variables in formulas?

    Maybe try something like:
    Please Login or Register  to view this content.
    Regards, TMS

  5. #5
    Registered User
    Join Date
    08-09-2014
    Location
    Finland
    MS-Off Ver
    2013
    Posts
    4

    Re: Question: how to use variables in formulas?

    Simple replacement didn't work. I had to make second variable which is defined as string:

    Dim MaxTime2 As String
    MaxTime2 = Replace(MaxTime, ",", ".")

    When I use this sting variable in formula, it works! Changing language settings from windows also helps but this solution is much better.

    Case closed, solution found. Thanks TMS!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Question: how to use variables in formulas?

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Question: how to use variables in formulas?

    "Case closed, solution found" could someone mark this as solved I read through all that to see if I could help & its solved.

  8. #8
    Registered User
    Join Date
    08-09-2014
    Location
    Finland
    MS-Off Ver
    2013
    Posts
    4

    Re: Question: how to use variables in formulas?

    Sorry, I'm new here and didn't know the policy...

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Question: how to use variables in formulas?

    @Osmond: no problem, thanks for the rep.

    @dlow: It is the responsibility of the OP to mark a thread solved, but, as in this case, new members are not always familiar with the etiquette and rules of the forum. Sometimes a Moderator or Administrator will mark a thread closed based on the content of the post(s), but not always.

    But, just because a thread is solved and/or marked solved, it doesn't mean you can't contribute if you have an alternative and/or better solution. In this case, we have come up with a compromise. However, I'm not convinced it is the only, or the best, solution. It does, though, solve the immediate problem in the absence of any other offerings.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Vlookup (or other formulas) to use two variables
    By amartino44 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-12-2013, 10:24 AM
  2. Excel 2007 : IF formulas: possible to use variables?
    By Tsuchigo in forum Excel General
    Replies: 5
    Last Post: 03-09-2011, 10:20 PM
  3. Variables in Excel Formulas
    By smokeymountain in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-11-2010, 07:16 PM
  4. How can I use variables in formulas in VB?
    By thorsten in forum Excel General
    Replies: 2
    Last Post: 05-16-2006, 03:30 PM
  5. VBA question concerning variables
    By SWT in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-21-2005, 04:05 PM

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