如何将公式分配给匿名形状?
How to assign formula to an anonymous shape?
我正在尝试将公式分配给对象,但每次都出现错误。要点是,每次我复制图像时,我都需要为新发布的项目分配一个公式。事实上,我没有具体的形状名称,所以我可以使用。有什么建议吗?
Dim sh As Shape
Set sh = ActiveSheet.range("A" & Last_row ).Shape
sh.DrawingObject.Formula = "=IMAGE" & Last_row
不能直接使用范围参考来识别形状。这个例子所做的是查看指定 sheet 中的所有形状......它找到第一个形状,其中形状的左上角位于指定的单元格中(即本例中的 C2)。 .. 它 returns 那个形状,以便可以为其分配一个公式(在本例中为“=B1”)。您应该能够接受它并将其扩展为您正在尝试做的事情。
Option Explicit
Public Sub AssignShapeFormulaExample()
Dim vShape As Shape
Dim vRange As Range
Dim vSheet As Worksheet
' Setup objects for the active sheet and an example cell (where a shape exists)
Set vSheet = ActiveSheet
Set vRange = vSheet.Range("C2")
Set vShape = FirstShapeInCell(vSheet, vRange)
If Not (vShape Is Nothing) Then
vShape.DrawingObject.Formula = "=B1"
End If
End Sub
Function FirstShapeInCell(vSheet As Worksheet, vRange As Range) As Shape
Dim vShape As Shape
Dim vShapeTopLeft As Range
Dim vIntersect As Range
' Loop though all shapes in the designated sheet
For Each vShape In vSheet.Shapes
' Setup a range that contains the top left corner of the shape
With vShape
Set vShapeTopLeft = vSheet.Cells(.TopLeftCell.Row, .TopLeftCell.Column)
End With
'See whether the shape in the range specified as an input parameter
Set vIntersect = Application.Intersect(vRange, vShapeTopLeft)
If Not (vIntersect Is Nothing) Then
Set FirstShapeInCell = vShape
Exit Function
End If
Next
End Function
我正在尝试将公式分配给对象,但每次都出现错误。要点是,每次我复制图像时,我都需要为新发布的项目分配一个公式。事实上,我没有具体的形状名称,所以我可以使用。有什么建议吗?
Dim sh As Shape
Set sh = ActiveSheet.range("A" & Last_row ).Shape
sh.DrawingObject.Formula = "=IMAGE" & Last_row
不能直接使用范围参考来识别形状。这个例子所做的是查看指定 sheet 中的所有形状......它找到第一个形状,其中形状的左上角位于指定的单元格中(即本例中的 C2)。 .. 它 returns 那个形状,以便可以为其分配一个公式(在本例中为“=B1”)。您应该能够接受它并将其扩展为您正在尝试做的事情。
Option Explicit
Public Sub AssignShapeFormulaExample()
Dim vShape As Shape
Dim vRange As Range
Dim vSheet As Worksheet
' Setup objects for the active sheet and an example cell (where a shape exists)
Set vSheet = ActiveSheet
Set vRange = vSheet.Range("C2")
Set vShape = FirstShapeInCell(vSheet, vRange)
If Not (vShape Is Nothing) Then
vShape.DrawingObject.Formula = "=B1"
End If
End Sub
Function FirstShapeInCell(vSheet As Worksheet, vRange As Range) As Shape
Dim vShape As Shape
Dim vShapeTopLeft As Range
Dim vIntersect As Range
' Loop though all shapes in the designated sheet
For Each vShape In vSheet.Shapes
' Setup a range that contains the top left corner of the shape
With vShape
Set vShapeTopLeft = vSheet.Cells(.TopLeftCell.Row, .TopLeftCell.Column)
End With
'See whether the shape in the range specified as an input parameter
Set vIntersect = Application.Intersect(vRange, vShapeTopLeft)
If Not (vIntersect Is Nothing) Then
Set FirstShapeInCell = vShape
Exit Function
End If
Next
End Function