首页 > 解决方案 > 需要 VBA 代码将形状(例如:向下箭头/向上箭头)放置在 power point 幻灯片中表格的特定单元格(基于单元格值)的顶部

问题描述

根据 Power Point 表中单元格的值,我需要帮助编写用于在单元格上放置形状的 VBA 代码。假设如果我有一张幻灯片中有 3 行 3 列的表格,并且我的表格在每个单元格中都包含一些随机数。如果单元格值小于 5,那么我想使用 VBA 代码在单元格顶部添加或放置一个向下箭头形状 (↓)。

Sub SetTableFont1()
Dim oShp As Shape
Dim oTbl As Table
Dim oSld As Slide
Dim I As Long
Dim J As Long

For Each oSld In ActivePresentation.Slides
For Each oShp In oSld.Shapes
If oShp.HasTable Then

Set oTbl = oShp.Table
For I = 1 To oTbl.Columns.Count
For J = 1 To oTbl.Rows.Count

ActivePresentation.Slides(1).Shapes("Down").Copy
' I have copied a down arrow shape which is manually added in the slide 
  and i named it as "Down"

'Now I am going to select the cells of the table and i want to paste that 
 copied down arrow shape on top of the selected cell.
 oTbl.Cell(J, I).Select
 If oTbl.Cell(J, I).Value <= 5 Then
 oTbl.Cell(J, I).Paste
 End If

 Next J
 Next I
 End If
 Next oShp
 Next oSld
 End Sub

 Please help me on above code.
 Below is screenshot for Reference

在此处输入图像描述

标签: vbapowerpoint

解决方案


粘贴到单元格中是行不通的。理论上,您可以获取表格的位置和尺寸,然后创建一个包含行宽和列高的二维数组。然后在找到目标单元格后,计算其位置并将箭头设置为这些坐标。但是,如果任何单元格被合并或细分,您将得到不正确的结果,因为在 VBA 中没有任何简单的方法可以识别这些单元格。


推荐阅读