+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    10-27-2009
    Location
    Wilmington, DE
    MS-Off Ver
    Excel 2007
    Posts
    2

    VBA Function Formula Using Sum and If

    I am using Excel 2007 on Windows XP. I have the following formula that I would like to move from the worksheet into VBA. I am having an issue with the “IF” statement within VBA throwing a compile error for “Expected: expression”. I am assuming it does not like the “IF” statement within the formula. I also provided a sample of the code that I was able to get working with a SUMIF statement but with only one condition. Any help would be appreciated. Thank you.

    Code:
    =SUM(IF(Log!$A:$A=$A3,IF(Log!$B:$B=$B3,Log!$H:$H)))
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Code:
    Function LOE(ProjectID As Double, ProjectIndicators As String)
        Dim xProjectIndicators As String, CurrentProjectIndicator As String
        Dim zPosition As Long, yLengthOfIndicator As Long
        Dim Sumact As Range
    
        xProjectIndicators = ProjectIndicators
        yLengthOfIndicator = Len(xProjectIndicators)
        Set logFirstProjID = Worksheets("Log").Range("A1:A50000")
        Set logProjectIndicator = Worksheets("Log").Range("B1:B50000")
        Set logHours = Worksheets("Log").Range("H1:H50000")
        
        For zPosition = 1 To yLengthOfIndicator
            CurrentProjectIndicator = Mid(xProjectIndicators, zPosition, 1)
            answer = Application.WorksheetFunction. Sum(If(logFirstProjID = ProjectID,If(logProjectIndicator = CurrentProjectIndicator, logHours)))
            LOE = LOE + answer
        Next
            
    End Function
    Last edited by phooey123; 10-27-2009 at 02:41 PM.

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: VBA Function Formula Using Sum and If

    Any particular reason you're not using the native SUMIFS function ?
    (new to XL2007)

    Your present formula would need to be an Array and the SUMIFS function will be far more efficient esp. if used over entire column references.

  3. #3
    Registered User
    Join Date
    10-27-2009
    Location
    Wilmington, DE
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: VBA Function Formula Using Sum and If

    I used DonkyOte's suggestions which included using the SUMIFS and also read through the the Recommended Reading for Sumproduct which was an excelent source. Here is the solution that was used that worked perfectly.

    Code:
    Function LOE(ProjectID As Double, ProjectIndicators As String, EstimatePhase As String, BlockID As String)
        Dim xProjectIndicators As String, CurrentProjectIndicator As String
        Dim zPosition As Long, yLengthOfIndicator As Long
        Dim Sumact As Range
    
        xProjectIndicators = ProjectIndicators
        yLengthOfIndicator = Len(xProjectIndicators)
        Set logFirstProjID = Worksheets("Log").Range("A1:A50000")
        Set logProjectIndicator = Worksheets("Log").Range("B1:B50000")
        Set logBlock = Worksheets("Log").Range("D1:D50000")
        Set logType = Worksheets("Log").Range("F1:F50000")
        Set logHours = Worksheets("Log").Range("H1:H50000")
        Set logCost = Worksheets("Log").Range("I1:I50000")
        
        For zPosition = 1 To yLengthOfIndicator
            CurrentProjectIndicator = Mid(xProjectIndicators, zPosition, 1)
            answer = Application.WorksheetFunction.SumIfs(logHours, logFirstProjID, ProjectID, logProjectIndicator, CurrentProjectIndicator, logType, EstimatePhase, logBlock, BlockID)
            '=SUMIFS(Log!$H:$H,Log!$A:$A,$A3,Log!$B:$B,$B3,Log!$F:$F,$J8,Log!$D:$D,P$1)
            LOE = LOE + answer
        Next
            
    End Function

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