首页 > 解决方案 > 整个工作簿的 SUMIF

问题描述

一直在寻找解决此问题的工作电子表格,但尚未找到解决方案。我的工作簿目前包括每个客户在个人工作表上的每日股票活动(工作表标题为 '5.9.2018、6.9.2018、7.9.2018 等)。该工作簿还包含一个主工作表,旨在提供这些每日工作表的每周和每月摘要(下面的主工作表屏幕截图):

掌握

我想做的是将 sumifs 或类似功能合并到我的 vba 中,它将使用主表中的客户名称作为参考,并检查每个每日工作表中的预定列以生成每月和每周的数据。任何有关如何将其合并到 VBA 中的指导将不胜感激,因为我是 VBA 的相对新手,并且无法自己找到解决方案。

谢谢

编辑:当前的 VBA 包括在下面

Sub AutoSum()
    Dim wscount As Long
    Dim i As Long
    wscount = ActiveWorkbook.Worksheets.Count

    For i = 1 To wscount
        Sheets(i).Select
        Range("K4").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(2, 0).Select

        Dim cel1 As String, cel2 As String
        cel1 = ActiveCell.Offset(-2, 0).End(xlUp).Address
        cel2 = ActiveCell.Offset(-1).Address
        ActiveCell.Value = "=sum(" & (cel1) & ":" & (cel2) & ")"
    Next i

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next

    Set rng = Selection.SpecialCells(xlCellTypeVisible)

    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
           vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To =
        .CC = ""
        .BCC = ""
        .Subject = "Today's Trades" & Date
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    TempWB.Close savechanges:=False

    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

标签: excelvbasumifs

解决方案


我认为你甚至不需要 VBA 来完成这项任务。像这样的东西应该为你做。

=SUMPRODUCT(SUMIF(INDIRECT("'"&sheets&"'!"&"rng"),criteria,INDIRECT("'"&sheets&"'!"&"sumrng")))

在此处输入图像描述


推荐阅读