如果你知道它的 topleftcell 行和列,你能直接 select 一个形状吗
Can you select a shape directly, if you know its topleftcell row & column
我有大约。 sheet 上有 100 个矩形。我想更改我知道其 TopLeftCell
坐标的特定矩形的颜色。
我希望能够直接 select 这个矩形来改变它的颜色,但是我找不到任何 VBA 代码来做到这一点。目前,我能找到的唯一代码是 selects sheet 上的所有形状,然后寻找 sheet 上的每个形状与 TopLeftCell
上的每个形状的交集, 然后select那个矩形改变它的颜色。
可能有 100 个形状要检查,这似乎是一种非常低效的方法,我认为必须有更好的方法。
Dim sh as shape
For Each sh In ActiveSheet.Shapes
If Not Intersect(Cells(RowNumber, ColumnNumber), sh.TopLeftCell) Is Nothing Then
sh.Select False
Selection.Interior.ColorIndex = 3
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
End If
Next sh
我想知道是否有像
这样的代码
selection.shape.topleftcell(cells(RowNumber,ColumnNumber))
或类似的可能出现在 VBA.
我尝试了这个和其他类似的代码,但都给出了错误。
运行 a loop
像这样一次将 Rectangles
的名称更改为他们 TopLeftCell
的地址
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
sh.Name = sh.TopLeftCell.Address
Next sh
现在在任何其他代码中,您可以使用以下方法直接访问形状:
ActiveSheet.Shapes(ActiveCell.Address).Select
这是实现它的一种方法。虽然没有您要找的方法。
您可以更改 ActiveCell.Address
任何范围对象或只是文本本身。它将采用 $D
这样的值
久经考验,运行顺畅。
如果您所做的只是Select
改变您想要改变颜色的形状,那么只需:
Sub changeColor()
Selection.Interior.ColorIndex = 3
End Sub
如果您想以更有条理的方式访问 Shape 的其他属性,我建议在字典中收集 Shape 名称,并以 TopLeftCell 为键。然后你可以这样做:
Option Explicit
'Set Reference to Microsoft Scripting Runtime
Public dShapes As Dictionary
Private Sub refShapes()
Dim WS As Worksheet
Dim SH As Shape
Set WS = ActiveSheet
Set dShapes = New Dictionary
dShapes.CompareMode = TextCompare
For Each SH In WS.Shapes
dShapes.Add Key:=SH.topLeftCell.Address, Item:=SH.Name
Next SH
End Sub
Sub changeColor()
Dim SH As Shape
Dim topLeftCell As String
topLeftCell = Selection.topLeftCell.Address
refShapes
If dShapes.Exists(topLeftCell) Then
Set SH = ActiveSheet.Shapes(dShapes(topLeftCell))
SH.Fill.ForeColor.RGB = RGB(255, 0, 255)
SH.Fill.Visible = msoTrue
SH.Fill.Solid
Else
MsgBox ("No shape at that location")
End If
End Sub
但是,如果您有多个形状具有相同的 TopLeftCell
,此技术将失败,但如有必要,可以调整以处理这种情况。
我有大约。 sheet 上有 100 个矩形。我想更改我知道其 TopLeftCell
坐标的特定矩形的颜色。
我希望能够直接 select 这个矩形来改变它的颜色,但是我找不到任何 VBA 代码来做到这一点。目前,我能找到的唯一代码是 selects sheet 上的所有形状,然后寻找 sheet 上的每个形状与 TopLeftCell
上的每个形状的交集, 然后select那个矩形改变它的颜色。
可能有 100 个形状要检查,这似乎是一种非常低效的方法,我认为必须有更好的方法。
Dim sh as shape
For Each sh In ActiveSheet.Shapes
If Not Intersect(Cells(RowNumber, ColumnNumber), sh.TopLeftCell) Is Nothing Then
sh.Select False
Selection.Interior.ColorIndex = 3
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
End If
Next sh
我想知道是否有像
这样的代码selection.shape.topleftcell(cells(RowNumber,ColumnNumber))
或类似的可能出现在 VBA.
我尝试了这个和其他类似的代码,但都给出了错误。
运行 a loop
像这样一次将 Rectangles
的名称更改为他们 TopLeftCell
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
sh.Name = sh.TopLeftCell.Address
Next sh
现在在任何其他代码中,您可以使用以下方法直接访问形状:
ActiveSheet.Shapes(ActiveCell.Address).Select
这是实现它的一种方法。虽然没有您要找的方法。
您可以更改 ActiveCell.Address
任何范围对象或只是文本本身。它将采用 $D
久经考验,运行顺畅。
如果您所做的只是Select
改变您想要改变颜色的形状,那么只需:
Sub changeColor()
Selection.Interior.ColorIndex = 3
End Sub
如果您想以更有条理的方式访问 Shape 的其他属性,我建议在字典中收集 Shape 名称,并以 TopLeftCell 为键。然后你可以这样做:
Option Explicit
'Set Reference to Microsoft Scripting Runtime
Public dShapes As Dictionary
Private Sub refShapes()
Dim WS As Worksheet
Dim SH As Shape
Set WS = ActiveSheet
Set dShapes = New Dictionary
dShapes.CompareMode = TextCompare
For Each SH In WS.Shapes
dShapes.Add Key:=SH.topLeftCell.Address, Item:=SH.Name
Next SH
End Sub
Sub changeColor()
Dim SH As Shape
Dim topLeftCell As String
topLeftCell = Selection.topLeftCell.Address
refShapes
If dShapes.Exists(topLeftCell) Then
Set SH = ActiveSheet.Shapes(dShapes(topLeftCell))
SH.Fill.ForeColor.RGB = RGB(255, 0, 255)
SH.Fill.Visible = msoTrue
SH.Fill.Solid
Else
MsgBox ("No shape at that location")
End If
End Sub
但是,如果您有多个形状具有相同的 TopLeftCell
,此技术将失败,但如有必要,可以调整以处理这种情况。