我有一个包含多列的数据集,但是执行此代码需要3个重要的列。我想检查特定项目(在项目编号的列中)的内容(在是/否/(空白)列中)是否已更改。我想知道它从哪里改变了。
我尝试使用Excel公式执行此操作,但是由于两个原因,这不是一个选择:
-我只能使用=MINIFS([ReportDate],[ItemNum],[@ItemNum],[Content],"yes")
来确定产品何时更改过一次
-Excel公式求解对于上述公式花费了很长时间,因此不切实际
我开始使用VBA,因为它的速度要快得多,但是我认为我的代码效率不高,因为每次更改内容且有2,000多个项目时,显示所有内容大约需要10秒钟。
TableName = "DataTableTest"
Country = "GB"
ItemNum = "1265845943"
Set CalcRange = Cells(2,3)
DateFormat = Format(ReportDate,"yyyy-mm-dd")
ContentTemp = "n/a"
startDate = DateValue("12 / 5 / 2019")
endDate = DateValue("14 / 12 / 2019")
i = 1
For ReportDate = startDate To endDate
DateFormat = Format(ReportDate,"yyyy-mm-dd")
ContentBlank = ""
CalcRange.FormulaR1C1 = _
"=IFERROR(GETPIVOTDATA(""[Measures].[Sum of UnitsSales]"",R3C1,""[" & TableName & "].[content]"",""[" & TableName & "].[content].&[" & ContentBlank & "]"",""[" & TableName & "].[ItemNum]"",""[" & TableName & "].[ItemNum].&[" & ItemNum & "]"",""[" & TableName & "].[ReportDate]"",""[" & TableName & "].[ReportDate].&" & _
"[" & DateFormat & "T00:00:00]"",""[" & TableName & "].[countryCode]"",""[" & TableName & "].[countryCode].&[" & Country & "]""),FALSE)" & _
""
Blank = CalcRange.Value
ContentYes = "yes"
CalcRange.FormulaR1C1 = _
"=IFERROR(GETPIVOTDATA(""[Measures].[Sum of UnitsSales]"",""[" & TableName & "].[content].&[" & ContentYes & "]"",FALSE)" & _
""
Yes = CalcRange.Value
ContentNo = "no"
CalcRange.FormulaR1C1 = _
"=IFERROR(GETPIVOTDATA(""[Measures].[Sum of UnitsSales]"",""[" & TableName & "].[content].&[" & ContentNo & "]"",FALSE)" & _
""
No = CalcRange.Value
CalcRange.Clear
If Blank = False And No = False And Yes = False Then
GoTo NextIteration
End If
If Blank = False And No = False Then
Content = "Yes"
ElseIf Yes = False And No = False Then
Content = "n/a"
ElseIf Blank = False And Yes = False Then
Content = "No"
End If
If Not ContentTemp = Content Then
MsgBox "Content changed from " & ContentTemp & " to " & Content & " on " & ReportDate
End If
ContentTemp = Content
NextIteration:
Next
如果我在正确的轨道上,有人可以帮我
谢谢 CMike