在单独的电子表格中使用范围上的Sumif

时间:2011-06-16 12:54:51

标签: excel excel-vba vba

我目前正在使用以下代码

执行Sumifs计算
 Public Function WFPAID(rev_date As Date) As Variant

  Application.Volatile (True)

  Set Vstatus = Sheets("KRONOS").Range("$DL:$DL")
  Set Team = Sheets("KRONOS").Range("$DO:$DO")
  Set WF_Paydate = Sheets("KRONOS").Range("$DK:$DK")

            WFPAID = Application.WorksheetFunction.SumIfs( _
            Writer_Fee _
            , Team, "<>9" _
            , Vstatus, "<>rejected", Vstatus, "<>unverified" _
            , WF_Paydate, rev_date)

 End Function

我的Excel文件名称为DATADUMP,并保存在我们的服务器上以下位置U:\DATADUMP.xlsx。我想在U:\DATADUMP.xlsx表中预先形成Sumifs计算。来自名为KRONOS的其他Excel的NEWCAL

请帮助。

3 个答案:

答案 0 :(得分:1)

将此代码粘贴到NEWCALC.xlsx中的模块中并作为宏运行。它会将SumIfs计算的结果输出到NEWCALC中的单元格。

Sub CalculateSumIfsOnDATADUMP()

Dim wb As Workbook
Dim Vstatus As Range, Team As Range, WF_Paydate As Range, Writer_Fee As Range
Dim WFPAID As Variant
Dim rng As Range
Dim colCOUNT As Long

    '// turn off screen updating
    Application.ScreenUpdating = False

    '// open the source workbook as read only
    Set wb = Workbooks.Open("U:\DATADUMP.xlsx", ReadOnly:=True)

    ThisWorkbook.ActiveSheet.Activate

    For Each rng In Application.Selection
    rev_date = rng.Value

        With wb.Worksheets("KRONOS")

            '// Set range variables for SumIfs calculation
            '// (NOTE: 'Writer_Fee' range needs to be supplied)
            Set Writer_Fee = .Range("Something")
            Set Vstatus = .Range("$DL:$DL")
            Set Team = .Range("$DO:$DO")
            Set WF_Paydate = .Range("$DK:$DK")

            '// Do the SumIfs Calculation, and store in WFAID
            WFPAID = Application.WorksheetFunction.SumIfs(Writer_Fee, _
                                                          Team, "<>9", _
                                                          Vstatus, "<>rejected", _
                                                          Vstatus, "<>unverified", _
                                                          WF_Paydate, "=" & rev_date)
        End With

        '// Output the calculation result to a cell in the ActiveSheet
        ActiveSheet.Cells(rng.Row, rng.Column + Selection.Columns.Count) = WFPAID

    Next rng

    '// close the source workbook without saving any changes
    wb.Close False

    '// turn on screen updating
    Application.ScreenUpdating = True

End Sub

修改
我更改了代码以读取所选范围,并将SumIfs结果输出到它旁边的列中。

答案 1 :(得分:0)

假设“DATADUMP.xlsx”已经打开,您可以尝试这样的事情:

Public Function WFPAID(rev_date As Date) As Variant
   Dim objWorksheet As Excel.Worksheet

    Dim objWriterFee As Excel.Range
    Dim objStatus As Excel.Range
    Dim objTeam As Excel.Range
    Dim objPaydate As Excel.Range

    Application.Volatile (True)

    ' This assumes "DATADUMP.xlsx" is already open.
    Set objWorksheet = Application.Workbooks("DATADUMP.xlsx").Worksheets("KRONOS")

    ' Change the next line to set the "writer fee" to a valid range.
    Set objWriterFee = objWorksheet.Range("?")
    Set objStatus = objWorksheet.Range("$DL:$DL")
    Set objTeam = objWorksheet.Range("$DO:$DO")
    Set objPaydate = objWorksheet.Range("$DK:$DK")

    WFPAID = _
        Application.WorksheetFunction.SumIfs _
        ( _
             objWriterFee, _
             objTeam, "<>9", _
             objStatus, "<>rejected", objStatus, "<>unverified", _
             objPaydate, rev_date _
        )
End Function

但是,如果“DATADUMP.xlsx”已经打开,则在重新计算期间无法在当前应用程序中打开它。

答案 2 :(得分:0)

我想发布另一种方法来做到这一点,我不知道是否可能。根据{{​​3}},你可以:

  

使用公式中的工作簿名称调用另一个(打开的)工作簿中包含的UDF。例如:='MyBook.xls'!RectangleArea(A1,A2)

所以这意味着调用UDF的另一种方法是确保其他WB已打开:Workbooks.Open("U:\DATADUMP.xlsx", ReadOnly:=True)

然后在另一个工作表中,确保该函数如下所示:     ='DATADUMP.xlsx'!WFPAID([Cells reference to dat])

这似乎是实现目标的一种更简单的方法。如果需要,您可以随附vba,但您也可以在工作表计算时限制,或者执行copy - pasteValues以保留结果。