+ Reply to Thread
Results 1 to 6 of 6

Change Formula to Static Value in VB

  1. #1
    Registered User
    Join Date
    06-02-2009
    Location
    Arlington, VA
    MS-Off Ver
    Excel 2007
    Posts
    80

    Change Formula to Static Value in VB

    I have the following code that places a formula into the cell.

    Please Login or Register  to view this content.
    skip is a long type variable which determines the number of rows to count down in the range.

    How can I change this to place only the result of the formula into the cell?
    Last edited by nsorden; 08-19-2009 at 12:36 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Change Formula to Static Value in VB

    Maybe just another line, plus you used the same cell for both halves of a range, which is overkill, just list the cell:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717

    Re: Change Formula to Static Value in VB

    Use inbuilt fucntion application.worksheetfunction.sum

    eg.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-02-2009
    Location
    Arlington, VA
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Change Formula to Static Value in VB

    With relay.Cells(k, 7)
    .FormulaArray = "=SUM((R[1]C[-1]:R" & Skip & "C[-1]/RC[-1])*(R[1]C:R" & Skip & "C))"
    .Value = .Value
    End With
    Thanks JBeaucaire, the use of a single cell at the top is helpful. The fomula does need to reference the range in the formula because it is calculating a weighted average.

    I have used the below solution for the static value, but I am having trouble making the loop shift from column to column.

    Please Login or Register  to view this content.
    Is there a way to combine these into one statement? Any ideas?
    Please Login or Register  to view this content.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Change Formula to Static Value in VB

    Quote Originally Posted by nsorden View Post
    Thanks JBeaucaire, the use of a single cell at the top is helpful. The fomula does need to reference the range in the formula because it is calculating a weighted average.
    You missed my point. You're misusing the RANGE in VBA. It should be Range(1stcell,2nd cell). You're noting the same cell twice doesn't do anything noteworthy, so there's no need to list it that way.
    Please Login or Register  to view this content.
    As for combining loops, of course. You can simply use one loop variable, you don't need multiples. Without seeing the sheet this applying to or knowing what's really happening with the code, I can't honestly say whether or not the "else" part could be combined like I know the first part can be:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-02-2009
    Location
    Arlington, VA
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: Change Formula to Static Value in VB

    That works great, thanks again. I changed all of those range references as well.

+ 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