需要VBA代码在Powerpoint幻灯片中的表格的特定单元格(基于单元格值)的顶部放置形状(例如:向下箭头/向上箭头)

时间:2019-07-04 13:02:10

标签: vba powerpoint

基于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

enter image description here

1 个答案:

答案 0 :(得分:0)

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