-1

Have been looking to solve this issue for a work spreadsheet but yet to find a solution. My workbook currently includes daily stock activity per client on individual worksheets (worksheets titled '5.9.2018, 6.9.2018, 7.9.2018 and so forth). The workbook also contains a master worksheet which is designed to provide weekly and monthly summaries of these daily worksheets (screenshot of master sheet below):

master

What I would like to do is incorporate a sumifs or similar function into my vba which will use the client name from the master sheet as a reference and check pre-determined columns across each daily worksheet to produce those monthly and weekly figures. Any guidance on how to incorporate this into a VBA would be greatly appreciated as I am a relative newcomer to VBA and have not been able to find the solution on my own.

Thank you

Edit: Current VBA included below

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
NHure92
  • 105
  • 2
  • 9
  • Please include the code you already have tried in your question. Also it is necessary that you ask a question to that code and tell where you got stuck or errors. Reading [ask] might help to improve your question. • Also why do you need VBA if there is a `=SUMIFS` formula you can use? – Pᴇʜ Nov 07 '18 at 09:02
  • Hi, I've edited and included my code. My code currently provides a sum total for column K on each worksheet and embeds the active worksheet into an outlook email and sends it out. As for codes for sumif, I've been reading but have been unable to find anything thus far which is why I'm here looking for possible solutions. The VBA has been requested by management, it is out of my control – NHure92 Nov 07 '18 at 09:12
  • First of all you should read and apply: [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) • At least for me it is still very unclear what exactly you try to sum (to less information, see also [mcve]). But have a look at the [WorksheetFunction.SumIfs Method](https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.sumifs) – Pᴇʜ Nov 07 '18 at 09:28
  • 1
    Possible duplicate of [VBA - sum totals to a master worksheet](https://stackoverflow.com/questions/52850307/vba-sum-totals-to-a-master-worksheet) – OwlsSleeping Nov 07 '18 at 10:13
  • 1
    The same question has been asked daily for weeks. If this is materially different to those other questions please focus on the specific point that needs help. – OwlsSleeping Nov 07 '18 at 10:15

1 Answers1

0

I don't think you even need VBA for this task. Something like this should do it for you.

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

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200