我在excel中有一个表格,该表格有八列,其中包括年份,存款日期和总费用。
现在,如果存款日期在两个给定日期之间并且与nodeIntegration
年有关,那么我正在尝试从相应的费用列中提取值。我试图获取特定年份在两个给定日期之间存入的总费用。但是我没有得到期望的结果。请帮助。
我正在使用2019-20
语句和If and Else
循环。
For Each
答案 0 :(得分:1)
如果要使用VAB进行操作,我建议使用ADODB这样的示例
<link rel = "stylesheet" type = "text/css" href = "../other/home.css" />
假设您将数据存储在名为Sub ReadFromWorksheetADO()
' Goto Tools/Reference
' Add a reference to Microsoft ActiveX Data Objects
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;"";"
Dim query As String
query = "SELECT SUM([Total Fee]) FROM (SELECT * from [data$] " & _
" WHERE [Date of Deposit] BETWEEN #04/30/2018# AND #05/01/2020# And [Year]='2020-21')"
Dim rs As New ADODB.Recordset
With rs
.Open query, conn
Debug.Print "Sum: " & .Fields(0)
End With
End Sub
的工作表中
答案 1 :(得分:0)
您没有显示要获得的结果,但是可以使用Range.Autofilter
方法提取满足您要求的行。您可以轻松构建一种仅提取费用的方法。
例如:
Option Explicit
Sub getFees()
Dim myData As Range
Dim WS As Worksheet
Dim ldtCol As Long, lyrCol As Long
Dim lFeeCol As Long, lFee1Col As Long
Dim dFees As Double
Dim vFees As Variant, v As Variant
Dim rFilteredData As Range, rDest As Range
'"1 / 5 / 2019")) And (Cell.Value <= DateValue("30 / 4 / 2020
'note these dates below are in MDY format
Const startDt As Date = #5/1/2019#
Const endDt As Date = #4/30/2020#
Const applYr As String = "2020-21"
Set WS = ThisWorkbook.Worksheets("sheet1")
With WS.Cells
Set myData = .Find(what:="S.No.", after:=.Item(.Rows.Count, .Columns.Count), _
LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False)
If myData Is Nothing Then
MsgBox "Data table not found"
Else
Set myData = myData.CurrentRegion
End If
End With
With myData.Rows(1)
ldtCol = .Find(what:="Date of Deposit").Column - myData.Column + 1
lyrCol = .Find(what:="Year").Column - myData.Column + 1
lFeeCol = .Find(what:="Base Fee", lookat:=xlWhole).Column - myData.Column + 1
lFee1Col = .Find(what:="Base Fee2").Column - myData.Column + 1
End With
Application.ScreenUpdating = False
If WS.AutoFilterMode = True Then WS.AutoFilter.ShowAllData
With myData
Set rDest = .Cells(1, 1).Offset(0, 10)
rDest.Resize(columnsize:=.Columns.Count).EntireColumn.Clear
.AutoFilter field:=lyrCol, Criteria1:=applYr
.AutoFilter field:=ldtCol, Criteria1:=">=" & CDbl(startDt), _
Operator:=xlAnd, Criteria2:="<=" & CDbl(endDt)
Set rFilteredData = myData.SpecialCells(xlCellTypeVisible)
Set rDest = .Cells(1, 1).Offset(0, 10)
rDest.Resize(columnsize:=.Columns.Count).EntireColumn.Clear
rFilteredData.Copy rDest
.AutoFilter 'turn off filter
rDest.EntireColumn.AutoFit
With rDest.CurrentRegion
vFees = Union(.Columns(lFeeCol), .Columns(lFee1Col))
End With
For Each v In vFees
If IsNumeric(v) Then dFees = dFees + v
Next v
Application.ScreenUpdating = True
MsgBox "Total Fees for period: " & dFees
End With
End Sub