excel - 将 sumifs 函数添加到 VBA
问题描述
我当前的 Excel 工作簿包含每天结束时输入文件的股票交易数据。每天的交易活动都放在单独的工作表中,并且列表格布局始终保持一致,因为每天都输入相同的数据字段。我当前的 VBA 在激活时提供所有工作表上某些列的总和,并通过电子邮件发送活动工作表。
但是,我目前必须进行手动 sumif 输入,以便在每天结束时对某些列中的某些数据点求和。有没有办法将 sumifs 公式合并到 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
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
'Set rng = Sheets("YourSheet").Range("D4:D12").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 = "nabil.hure@dnb.no"
.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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
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"
'Copy the range and create a new workbook to past the data in
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
'Publish the sheet to a htm file
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
'Read all data from the htm file into RangetoHTML
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=")
'Close TempWB
TempWB.Close savechanges:=False
解决方案
推荐阅读
- c# - 确保在派生类中定义静态属性
- javascript - PWA:服务工作者已更新但未缓存文件
- java - 为什么我无法从这些代码中打印任何单词?
- javascript - Web 令牌通知上的 Firebase 错误
- react-native - 检查 AsyncStorage 中的现有密钥
- node.js - 组件之间的Angular 4 ngModel
- python - 在python中将列表中的数字相乘
- java - 如何在firebase中获取所有孩子的数据并将其显示到我的android应用程序中?
- datatable - 合并的数据表不要复制到新表单
- ios - 从一个位置移动到另一个位置后 UIButton 框架发生了变化