我想在SUMIF函数中使用过滤范围。我的WB中有一些命名范围(Sheet1中的“ uselist”是项目的一列,Sheet2中的“等分”是数量的一列,Sheet2中的“ dates”是日期为select [item] ,
[variant descr],[variant order],
left([variant descr],charindex('/', [variant descr]) - 1) AS [Dim_Colour],
CASE WHEN [variant order] like '%/SIZE' THEN SUBSTRING([variant descr], CHARINDEX('/', [variant descr]) +1, 100)
ELSE '' END AS [Dim_Size],
CASE WHEN [variant order] like '%/STYLE' THEN SUBSTRING([variant descr], CHARINDEX('/', [variant descr]) +1, 100)
ELSE '' END AS [Dim_Style]
from your_table_name
的一列)。
我的代码中的此公式按预期工作(即,将“使用列表”列中具有yyyy-mm-dd
的“等分试样”中的数量相加):
item=cell.Value
但是现在我希望将总和限制为在“日期”列中具有在Sheet1的“ O”列中报告的日期与现在之间的日期之间的行。我正在工作的“单元格”在Sheet1中。我修改了代码,但遇到了一些问题:
Cells(cell.Row, "K") = Application.SumIf(Range("uselist"), cell.Value, Range("aliquotes"))
我认为要用于宏的两个不同的工作表存在问题。
答案 0 :(得分:0)
尝试一下:
但是请注意您的MyRange
,因为它没有在过程中声明:
Option Explicit
Sub FilterSum()
'ActiveSheet.UsedRange.Select 'avoid using select
Dim cell As Range
Dim XNEWRANGE As Range
Dim ws As Worksheet 'reference your sheets to avoid using select
Set ws = ThisWorkbook.Sheets("MySheet") 'change MySheet for your working sheet name
Application.ScreenUpdating = False
With ws
For Each cell In MyRange.Columns("A") 'MyRange isn't declared or it's value given anywhere
'Add your filters first
.Range("dates").AutoFilter Field:=1, Criteria1:=">=" & Cells(cell.Row, "O"), Operator:=xlAnd, Criteria2:="<=" & Now
'Set your range using the visible cells from A2 to last row visible on column A
Set XNEWRANGE = .Range("A2", .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1)).SpecialCells(xlCellTypeVisible)
.Cells(cell.Row, "K") = Application.SumIf(XNEWRANGE, cell.Value, Range("aliquotes"))
Next cell
End With
Application.ScreenUpdating = True
End Sub