Excel + TSQL(在条件下突出显示单元格的macros)

我使用SQL Server 2012数据库。

每天早上,我会得到一个新的计算结果表。 格式是

productID – countryID – 2001 – 2002 – 2003

我有2000个产品,每个在200个国家。 大约400000行。

此外,我有以前的计算相同的格式表。

我的任务是比较新旧结果,并创build一个格式输出Excel文件:

计算 – productID – countryID – 2001 – 2002 – 2003

旧 – 1 – CA – 0.02 – 0.89 – 5.3

新 – 1 – CA – 0.03 – 0.90 – 5.3

所以我按照产品和国家进行比较,每年比较一下价值。

我的问题是,我需要突出显示结果是不同的更多的2%的单元格。

有谁知道如何做的伎俩?

非常感谢。

假设每一行都在旧的前面,你需要select新的行,并与直接在它们上面的结果进行比较。 最简单的方法是添加一个filter,过滤新的,select结果,添加条件格式,然后取消过滤结果。

'Select everything and add a filter Cells.Select Selection.AutoFilter ActiveSheet.Range("$A$1:$C$10").AutoFilter Field:=1, Criteria1:="New" 'Select the newly filtered results Cells.Select 'Apply a conditional format (substitute B2 for the first filtered cell, and B1 for the cell above it) Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=IF(B2>(B1*1.2),TRUE,FALSE)" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 End With Selection.FormatConditions(1).StopIfTrue = False 'Remove the filter Cells.Select Selection.AutoFilter 

创build一个新的macros,并粘贴上面的代码,它应该工作。

你可以用Excel公式来处理这个问题:

 ABC old 0.5 new 1 Alert ! old 0.9 new 0.91 old 0.1 new 0.15 Alert ! =IF(A2="new";IF(B2-B1>0.02;"Alert !";"");"") 

要突出显示单元格,可以使用此公式的条件格式(从菜单格式)

我会去数据透视表的路线。 这是dynamic的。 如果添加新信息,可轻松扩展。 而不是依赖于你的数据正确的顺序,你需要的是。 它也可以包含条件格式,这些条件格式将随着数据的扩展而扩展。

Sub RunCheck()

Dim eilute As Integer

 ActiveSheet.Range("A2").Select Selection.End(xlDown).Select eilute = Selection.Cells.Row For e = 2 To eilute For s = 8 To 72 If Cells(e, s) <> Cells(e + 1, s) Then Cells(e, s).Interior.Color = RGB(217, 217, 25) Cells(e + 1, s).Interior.Color = RGB(217, 217, 25) End If Next s e = e + 1 Next e 

结束小组