【问题标题】:Excel + TSQL (macros to highlight cells on a condition)Excel + TSQL(在条件下突出显示单元格的宏)
【发布时间】:2013-11-04 15:45:15
【问题描述】:

我使用 SQL Server 2012 数据库。

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

productID - countryID - 2001 - 2002 - 2003

我有 2000 种产品,每种产品分布在 200 个国家/地区。大约 400000 行。

我也有与之前计算相同格式的表格。

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

计算 - productID - countryID - 2001 - 2002 - 2003

旧 - 1 - CA - 0.02 - 0.89 - 5.3

新 - 1 - CA - 0.03 - 0.90 - 5.3

所以我按产品和国家/地区分组,并比较每年的价值。

我的问题是我需要突出显示结果差异超过 2% 的那些单元格。

有人知道怎么做吗?

非常感谢。

【问题讨论】:

  • 对不起,写得不太正确。我的任务是自动化这个过程。数据的差异将由另一个部门检查。我正在尝试实现流程自动化:数据库-> excel文件->用于比较和格式化excel文件的宏

标签: sql-server excel export-to-excel vba


【解决方案1】:

假设每一行前面都有旧行,您需要选择新行,并与它们正上方的结果进行比较。最简单的方法是添加过滤器,过滤新的,选择结果,添加条件格式,然后取消过滤结果。

'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

创建一个新的宏,并将上面的代码粘贴到其中,它应该可以工作。

【讨论】:

  • 但是是否可以编写一个存储过程,将数据库中的数据导出到 excel 文件中,并创建一个“作业”来运行一个用于比较格式的宏?这个想法是因为数据的变化应该由另一个部门检查,我不能每天比较是用手突出差异:) 我的想法至少是可能的吗?
  • 您应该能够设置 Excel 文件以从数据库中查询数据 - 不涉及存储过程。然后,运行宏来设置条件格式。 (您不需要宏,但我发现 Excel 有时会在数据刷新时忘记格式化,即使它设置为记住格式。)您使用的是什么版本的 Excel?
  • 简单一点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 End Sub
  • 您只是检查值是否相同。在原始帖子中,您需要查看是否存在大于指定值的差异。 (尽管在我的示例中我输入了错误的值并输入了 20% 而不是 2%)
  • 哦 :)) 对不起!写了旧代码!是的,你是对的,它应该是: If Cells(e + 1, s) 0 Then If 100 * (Abs(1 - Cells(e, s) / Cells(e + 1, s))) > 2 Then 。 ...
【解决方案2】:

您可以使用 Excel 公式处理此问题:

A           B       C

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 !";"");"")

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

【讨论】:

  • 我知道如何在 excel 中进行比较 :) 但想法是创建自动比较程序
【解决方案3】:

我会走数据透视表路线。它是动态的。如果添加新信息,可轻松扩展。并且不依赖于您的数据按照您需要的确切顺序。它还可以包含会随着您的数据而扩展的条件格式。

【讨论】:

    【解决方案4】:

    Sub RunCheck()

    将 eilute 设为整数

    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
    

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多