- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于数据透视表中显示的数据,我选择对数据表的某些部分应用条件格式以突出显示某些范围内的值。弄清楚如何以不同于小计数据的方式突出显示第二级行数据很有趣,但我能够解决它。我的 VBA 使用 Worksheet_PivotTableUpdate
事件触发,以便每当用户更改数据透视表字段时,条件格式都会相应更新。
当某些部分折叠时,此方法仍然有效:
当所有顶级部分都折叠时,会发生运行时错误,因此不会显示第二级行数据 (position=2)。
我收到以下错误:
我一直在寻找一种方法来检测所有第二位置行字段是否已折叠/隐藏/不可见/未钻孔,以便识别该条件并跳过格式化部分。但是,我还没有发现 PivotField
、PivotItem
或 PivotTable
的哪个方法或属性将为我提供该信息。
直接附加到工作表的事件代码是
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
ColorizeData
End Sub
因此,在单独的模块中,ColorizeData
的代码为
Option Explicit
Sub ColorizeData()
Dim staffingTable As PivotTable
Dim data As Range
Set staffingTable = ActiveSheet.PivotTables(PIVOT_TABLE_NAME)
Set data = staffingTable.DataBodyRange
'--- don't select the bottom TOTALS row, we don't want it colored
Set data = data.Resize(data.rows.count - 1)
'--- ALWAYS clear all the conditional formatting before adding
' or changing it. otherwise you end up with lots of repeated
' formats and conflicting rules
ThisWorkbook.Sheets(PIVOT_SHEET_NAME).Cells.FormatConditions.Delete
ThisWorkbook.Sheets(PIVOT_SHEET_NAME).Cells.ClearFormats
staffingTable.DataBodyRange.Cells.NumberFormat = "#0.00"
staffingTable.ColumnRange.NumberFormat = "mmm-yyyy"
'--- the cell linked to the checkbox on the pivot sheet is
' supposed to be covered (and hidden) by the checkbox itself
If Not ThisWorkbook.Sheets(PIVOT_SHEET_NAME).Range("D2") Then
'--- we've already cleared it, so we're done
Exit Sub
End If
'--- capture the active cell so we can re-select it after we're done
Dim previouslySelected As Range
Set previouslySelected = ActiveCell
'--- colorizing will be based on the type of data being shown.
' Many times there will be multiple data sets shown as sums in
' the data area. the conditional formatting by FTEs only makes
' sense if we colorize the Resource or TaskName fields
' most of the other fields will be shown as summary lines
' (subtotals) so those will just get a simple and consistent
' color scheme
Dim field As PivotField
For Each field In staffingTable.PivotFields
Select Case field.Caption
Case "Project"
If field.Orientation = xlRowField Then
If field.Position = 1 Then
staffingTable.PivotSelect field.Caption, xlFirstRow, True
ColorizeDataRange Selection, RGB(47, 117, 181), RGB(255, 255, 255)
End If
End If
Case "WorkCenter"
If field.Orientation = xlRowField Then
If field.Position = 1 Then
staffingTable.PivotSelect field.Caption, xlFirstRow, True
ColorizeDataRange Selection, RGB(155, 194, 230), RGB(0, 0, 0)
End If
End If
Case "Resource"
If field.Orientation = xlRowField Then
If field.Position = 1 Then
staffingTable.PivotSelect field.Caption, xlFirstRow, True
Else
===> ERROR HERE--> staffingTable.PivotSelect field.Caption, xlDataOnly, True
End If
ColorizeConditionally Selection
End If
Case "TaskName"
If field.Orientation = xlRowField Then
If field.Position = 1 Then
staffingTable.PivotSelect field.Caption, xlFirstRow, True
Else
staffingTable.PivotSelect field.Caption, xlDataOnly, True
End If
ColorizeConditionally Selection
End If
End Select
Next field
'--- re-select the original cell so it looks the same as before
previouslySelected.Select
End Sub
表格的具体设置是当用户选择行数据为
以防万一您想知道,为了完整起见,我在此处包含了两个私有(private)子调用:
Private Sub ColorizeDataRange(ByRef data As Range, _
ByRef interiorColor As Variant, _
ByRef fontColor As Variant)
data.interior.Color = interiorColor
data.Font.Color = fontColor
End Sub
Private Sub ColorizeConditionally(ByRef data As Range)
'--- light green for part time FTEs
Dim dataCondition As FormatCondition
Set dataCondition = data.FormatConditions.Add(Type:=xlCellValue, _
Operator:=xlBetween, _
Formula1:="=0.1", _
Formula2:="=0.5")
With dataCondition
.Font.ThemeColor = xlThemeColorLight1
.Font.TintAndShade = 0
.interior.PatternColorIndex = xlAutomatic
.interior.ThemeColor = xlThemeColorAccent6
.interior.TintAndShade = 0.799981688894314
.SetFirstPriority
.StopIfTrue = False
End With
'--- solid green for full time FTEs
Set dataCondition = data.FormatConditions.Add(Type:=xlCellValue, _
Operator:=xlBetween, _
Formula1:="=0.51", _
Formula2:="=1.2")
With dataCondition
.Font.ThemeColor = xlThemeColorLight1
.Font.TintAndShade = 0
.Font.Color = RGB(0, 0, 0)
.interior.PatternColorIndex = xlAutomatic
.interior.Color = 5296274
.SetFirstPriority
.StopIfTrue = False
End With
'--- orange for slightly over full time FTEs
Set dataCondition = data.FormatConditions.Add(Type:=xlCellValue, _
Operator:=xlBetween, _
Formula1:="=1.2", _
Formula2:="=1.85")
With dataCondition
.Font.Color = RGB(0, 0, 0)
.Font.TintAndShade = 0
.interior.PatternColorIndex = xlAutomatic
.interior.Color = RGB(255, 192, 0)
.SetFirstPriority
.StopIfTrue = False
End With
'--- red for way over full time FTEs
Set dataCondition = data.FormatConditions.Add(Type:=xlCellValue, _
Operator:=xlGreater, _
Formula1:="=1.85")
With dataCondition
.Font.Color = RGB(255, 255, 255)
.Font.TintAndShade = 0
.interior.PatternColorIndex = xlAutomatic
.interior.Color = RGB(255, 0, 0)
.SetFirstPriority
.StopIfTrue = False
End With
End Sub
EDIT: thanks to @ScottHoltzman, I incorporated his check with the logic below and arrived a solution
Case "Resource"
If field.Orientation = xlRowField Then
If (field.Position = 2) And PivotItemsShown(staffingTable.PivotFields("Project")) Then
staffingTable.PivotSelect field.Caption, xlDataOnly, True
ColorizeConditionally Selection
ElseIf field.Position = 1 Then
staffingTable.PivotSelect field.Caption, xlFirstRow, True
ColorizeConditionally Selection
End If
End If
最佳答案
使用PivotItems
对象的ShowDetail
方法。我包装成一个函数,以便更清晰地集成到您的代码中。所有这些都是因为您必须测试该字段的每一项。
测试代码:
If field.Orientation = xlRowField Then
If PivotItemsShown(field) Then
If field.Position = 1 Then
staffingTable.PivotSelect field.Caption, xlFirstRow, True
Else
staffingTable.PivotSelect field.Caption, xlDataOnly, True
End If
ColorizeConditionally Selection
End If
End If
Function PivotItemShown(pf as PivotField) as Boolean
Dim pi as PivotItem
For each pi in pf.PivotItems
If pi.ShowDetail Then
PivotItemsShown = True
Exit For
End If
Next
End Function
更新:下面的两种黑客方法
既然您知道,在您的示例中,如果所有 3 个项目都折叠,单元格 A10 将为空白,您可以这样检查:
If Len(Range("A10") Then ... `skip this section
或者,如果您随时可能有动态项目列表,请使用此:
For each rng in Range(Range("A6"),Range("A6").End(xlDown))
If Instr(rng.Value,"Project") = 0 and rng.Value <> "Grand Total" Then
'.... select the row range as needed
Exit For
End If
Next
关于vba - 数据透视表 : Detect When Pivot Field is Collapsed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46608459/
我正在使用 xamarin 表单开发移动应用程序,是否可以检测 Android 中的滑动手势以进行屏幕截图或单击锁定屏幕或主屏幕? 如果可以通过 Objective C/swift/java 实现,那
我刚刚涉足计算机视觉领域,并试图揭开它的各种复杂性的神秘面纱。我正在尝试使用冲浪特征检测器来增强卡尔曼滤波器。但是我不明白在使用冲浪特征在检测到的帧上构造单应性和有界矩形后如何调用和使用卡尔曼方法。在
问题 我正在尝试使用 opencv2 来检测静止图像中的 PlayStation Move 运动 Controller 。为了增加球体和背景之间的对比度,我决定修改输入图像以自动缩放每个 channe
我正在制作一个 android 应用程序,它可以从从视频中捕获的图像帧中检测对象。 openCV中的示例应用只有实时检测的例子。 附加信息:-我正在使用 Haar 分类器 截至目前,我正在将捕获的帧存
我在我的测试应用程序中成功实现了 OpenCV 平方检测示例,但现在需要过滤输出,因为它非常困惑 - 还是我的代码错误? 我对论文的四个角点感兴趣,以减少偏斜(如 that)和进一步处理......
在我的应用程序中,我想对 UIImage 使用人脸检测,所以我使用库中的 CoreImage 构建(我知道 Vision Library 也是最新的人脸检测库。但它仅支持 ios 11.0 及以上)。
我的 Linux 3.0/glibc 2.13 应用程序因以下形式的错误而停止: *** glibc detected *** MYAPP: double free or corruption (fa
我正在尝试运行一个基本程序来检测用户是否安装了 window.ethereum。当我运行我的程序时,我得到“@metamask/detect-provider:无法检测到 window.ethereu
我正在使用 Haarcascades 检测人脸和眼睛。我的问题是,它像眼睛一样包围了许多盒子。我的语法是 face_cascade = cv2.CascadeClassifier('haarcasca
我如何检测用户在对话中点击“消息”? 如果 MessageViewController Controller 很紧凑并且用户向上滑动我如何检测到它? 我试过这些代表,但它不能正常工作 override
我在对象检测中使用 Microsoft 自定义视觉服务来提取需要的对象。我想做一个回归测试来比较结果。但是,我找不到一个地方可以导出带有用户通过 GUI 定义的边界框的训练图片。 模型训练是在 Mic
我对 chrome 进行了扩展。当我浏览到 https 下的网站时,我的链接仍在 http 下,我收到:“该站点使用 SSL,但 Google Chrome 检测到页面上存在高风险不安全内容或站点证书
我用的是JetBrains的Gogland工具来学习go语言,我安装成功了,但是打不开,现将报错列如下,有没有人遇到过这个问题?如能解决,将不胜感激。 错误: Java 运行时环境检测到 fatal
我想在 R 中评估和比较我的社区检测算法的结果。我的算法不允许重叠,并且有一些节点没有被处理。例如,对于 Zachary 空手道俱乐部,我有 1 个节点未处理。我找到了很多指标(NMI、ARI、Mod
是否有任何好的开源引擎来检测文本使用的语言,也许是概率度量?我可以在本地运行并且不查询 Google 或 Bing 的一个?我想在大约 1500 万页的 OCR 文本中检测每个页面的语言。 并非所有文
我正在开发一款 2D 游戏,其中包含高速射弹,这些射弹会撞击高速(并且可能旋转得非常快)的多边形目标对象。我一直在试验和寻找适合我的强大碰撞检测算法。 如果快速旋转不是一个因素(即 0 或慢速旋转),
我正在制作一款平台游戏,其中有玩家和一些 Prop 。为了检查碰撞,我使用了 matterjs collisionactive 函数: this.matter.world.on("collisiona
我已经习惯于使用矩形进行碰撞检测,现在我有点难住了。我正在处理类似菱形的形状,在过去的几个小时里,我一直在试图找出如何检查碰撞。 我尝试检查第一个对象的四个点是否在第二个对象的点内,但这只是一个框(我
最初,两个半径为 R1 和 R2 的非接触球体处于静止状态。 然后在时间 = 0 时分别给它们两个加速度 a1 和 a2。查明他们是否会接触。它们的初始位置分别表示为 (x1,y1,z1) 和 (x2
我目前正在学习使用 LWJGL 和 OpenGL 的 ThinMatrix 3d 游戏开发教程。我正在尝试在我的游戏中实现碰撞检测。我已经准备好检测 AABB 与 AABB 碰撞的代码,但似乎无法弄清
我是一名优秀的程序员,十分优秀!