Results 1 to 8 of 8

How to correct my combined SumIf and SumProduct calculation in macro coding

Threaded View

  1. #1
    Registered User
    Join Date
    06-11-2010
    Location
    Windsor, Canada
    MS-Off Ver
    Excel 2007
    Posts
    55

    Question How to correct my combined SumIf and SumProduct calculation in macro coding

    I was summing a column that was all Canadian funds but the client has since introduced other currencies into the rows. So now to sum a column if it is canadian funds then I just sum the funds and if it is other than CDN funds then I need to multiply by the currency then sum it all as Canadian. Here is my code below. Can anyone tell me why the SumProduct function will not work properly in my code. If I do the following command in Excel columns then everything works fine but now I need to take this code

    SUMIF($I$2:$I$6,"=CDN",P2:P6)+SUMPRODUCT(P2:P6,$J$2:$J$6)
    and incorporate it in my Excel Macro code where I look thru worksheets to sum each individual column that has numerical amounts. Am doing this to update the summary total worksheet. Here is my Excel Macro code below. I get a compile error on my new SummaryTotal field stating that "Sub or Function not defined" and it highlights the SumProduct function within my NEW SummaryTotal field calculation.

    The old SummaryTotal calculation worked just fine but when foreign currencies were introduced into the rows I had to modify the calculation. I did not want to loop thru the columns manually using a loop because each worksheet has maybe 30 columns that need to be individually summed and there are maybe 20 worksheets. My logic loops thru each worksheet and then summarizes the dollar totals for each individual column to a specific cell within the YTD summary total worksheet.

    How do I this modified SummaryTotal calculation to do what I need to do? Here is my macro coding below.

    ' Set up Start Column and end column to loop thru on YTD_Summary Worksheet
        StartColumn = "C3"
        EndColumn = "P3"
        
        ' Set up Start Row and End Row to loop thru on YTD_Summary Worksheet
        StartRow = 3
        EndRow = YTDSummarySheet.Range("YTDSummary_Accts_Footer").Row() - 1
        
        ' Loop thru the YTD_Summary Cost Centre columns
        For Each ColIndex In YTDSummarySheet.Range(StartColumn & ":" & EndColumn).Cells
            Current_Cost_Centre = YTDSummarySheet.Cells(2, ColIndex.Column).Value
            TempWork_Sheet = "CC_" & Current_Cost_Centre
            Set InvoiceLogSheet = Worksheets(TempWork_Sheet)
            lastInvoiceLogLine = InvoiceLogSheet.Range("CC_Footer_" & Current_Cost_Centre).Row()
    
            'Loop thru the YTD_Summary Object.Sub rows
            For RwIndex = StartRow To EndRow
                WorkObjSub = YTDSummarySheet.Cells(RwIndex, 1).Value
                
                ' If object/sub starts with "8" then
                ' look up object_Sub account to get appropriate column
                If Mid(WorkObjSub, 1, 1) = "8" Then
                    ColumnLetter = CheckForOBjSubColumn(WorkObjSub)
                
                    If ColumnLetter = "?" Then
                        Response = MsgBox("Object/Sub " & WorkObjSub & " cannot be found on CONTROL worksheet, Contact someone in ITS. Save of Invoice CANNOT continue.", vbOkay, "Calc Summary")
                        Exit Sub
                    End If
                
                    If ColumnLetter = "" Then
                        Response = MsgBox("Object/Sub: " & WorkObjSub & " in CONTROL worksheet has NO VALUE in Object Column.  Contact someone in ITS. Save of Invoice CANNOT continue.", vbOkay, "Calc Summary")
                        Exit Sub
                    End If
                Else
                    ' Use the string stored in the cell to look up
                    ' tax account so the tax column can be returned
                    WorkTaxName = WorkObjSub
                    ColumnLetter = CheckForTaxColumn(WorkTaxName, "SentAcct-WantCol")
                            
                    ' Check to see if a tax Column was returned
                    If ColumnLetter = "?" Then
                       Response = MsgBox("Tax Account: " & WorkTaxName & " in CONTROL worksheet has NO VALUE in G/L Tax Account Column.  Contact someone in ITS. Save of Invoice CANNOT continue.", vbOkay, "Calc Summary")
                       Exit Sub
                    End If
                End If
                
                ' Convert the Column Letter to a Column Number
                ' --------------------------------------------------
                Current_Cost_Centre = YTDSummarySheet.Cells(2, ColIndex.Column).Value
                TempWork_Sheet = "CC_" & Current_Cost_Centre
                Set sh = Sheets(TempWork_Sheet)
                ColumnNumber = sh.Cells(1, ColumnLetter).Column
                ColumnNumber_Currency = sh.Cells(1, "I").Column
                ColumnNumber_Exchange = sh.Cells(1, "J").Column
                
                ' Summarize a specific column for a specified Cost Centre
                '
                ' Without the Cells property being specifically qualified,
                ' the SUM method will apply only to the active sheet.
                '
                ' The Sum logic had to be slightly modfified to use a SumIf and SumProduct
                ' because we want to sum each column in total Canadian funds for that column.
                ' Any USD funds must be first converted to Canadian funds then summed together.
                '
                '=SUMIF($I$2:$I$6,"=CDN",P2:P6)+SUMPRODUCT(P2:P6,$J$2:$J$6)
                ' = SUMIF(CurrencyColRange,"=CDN",SumCurrentColRange) + SumProduct(CurrentColRange,ExchangeColRange)
                ' ---------------------------------------------------
                '(old command)  SummaryTotal = Application.WorksheetFunction. _
                                                Sum(Range(sh.Cells(2, ColumnNumber), sh.Cells(lastInvoiceLogLine, ColumnNumber)))
                
                SummaryTotal = Application.WorksheetFunction. _
                                SumIf(Range(sh.Cells(2, ColumnNumber_Currency), sh.Cells(lastInvoiceLogLine, ColumnNumber_Currency)), "=CDN", _
                                Range(sh.Cells(2, ColumnNumber), sh.Cells(lastInvoiceLogLine, ColumnNumber))) + _
                                SumProduct(Range(sh.Cells(2, ColumnNumber), sh.Cells(lastInvoiceLogLine, ColumnNumber)), _
                                Range(sh.Cells(2, ColumnNumber_Exchange), sh.Cells(lastInvoiceLogLine, ColumnNumber_Exchange)))
                
                YTDSummarySheet.Cells(RwIndex, ColIndex.Column).Value = SummaryTotal
            
            Next RwIndex
        Next ColIndex
            
        Range("YTDSummary_Title").Value = Range("FiscalYear_string").Value & " Year to Date Summary of each Cost Center by Column"
            
        Application.Calculate
    Last edited by cmwilbur; 08-19-2011 at 11:37 AM.
    cmwilbur

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