+ Reply to Thread
Results 1 to 4 of 4

Thread: Evaluate formula via VBA

  1. #1
    Registered User
    Join Date
    11-12-2009
    Location
    Lucca, Italy
    MS-Off Ver
    Excel 2003
    Posts
    35

    Evaluate formula via VBA

    I have the following formula that works fine:
    =IF(L1="",(SUMPRODUCT(--(spc=H1),--(mdate<=IF(K1="","*",K1)),--(msale))),(SUMPRODUCT(--(spc=H1),--(mdate>=K1),--(mdate<=L1),--(msale))))
    THe formula is used for many row so I have tried to insert the formula result via VBA but I have a sintax error

    Lrow2 = Range("H1").End(xlDown).Row
    For X = 1 To Lrow2
    Sheets("LC").Range("A" & Lrow) = eVALUATE("=IF(L"& x &"="",(SUMPRODUCT(--(spc=H"& x &"),--(mdate<=IF(K"& x &"="","*",K"& x &")),--(msale))),(SUMPRODUCT(--(spc=H"& x &"),--(mdate>=K"& x &"),--(mdate<=L"& x &"),--(msale))))")
    next X

    Any suggestion?
    Last edited by fabdav; 01-27-2012 at 04:12 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,958

    Re: Evaluate formula via VBA

    Are you trying to insert the formula into the cell, or only put the result of the formula into the cell?

    If the former you need to use the .Formula property of the range and the formula will have to be enclosed in quotes.

    If the latter then you can't just quote the formula, you'll have to translate it into VBA first.

    Working with a simpler formula - where you're inserting =B2+C2 into each row in A from 2 until the end of the sheet the two options would be:

    Sheets("LC").Range("A" & x).Formula="=SUM(B" & x & "+C" & x &")"
    Or

    Sheets("LC").Range("A" & x).Value = WorksheetFunction.Sum(Sheets("LC").Range("B" & x & ":C" & x))
    Hope that helps.

  3. #3
    Registered User
    Join Date
    11-12-2009
    Location
    Lucca, Italy
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Evaluate formula via VBA

    I want to insert the result of the formula.
    This is a simpler version but I have i have mismatch error:
    For X = 1 To 10
    P = Evaluate("=(SUMPRODUCT(--(spc=H" & X & "),--(mdate<=IF(K" & X & "=""," * ",K" & X & ")),--(msale)))")
    MsgBox P
    Next X
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,958

    Re: Evaluate formula via VBA

    Could you use code tags, please?

    I think the problem is with your quotation marks. Try:

    P = Evaluate("=(SUMPRODUCT(--(spc=H" & X & "),--(mdate<=IF(K" & X & ""="","" * "",K" & X & ")),--(msale)))")

+ 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.2.0