首页 > 解决方案 > 我可以在 vba 中使用 shape.width 作为条件吗

问题描述

我想在 if 中使用形状的宽度作为条件。我下面的代码会自动将数字添加到单元格范围内的形状中。

Sub numShapeMasqueD()

    Dim shapeTemp As Shape
    Dim masqueD As Range
    Dim cpt As Integer
    Dim row As Long, col As Long
    Set masqueD = Range("h22:r31")
    cpt = 1

    For row = 1 To masqueD.Rows.Count
        For col = 1 To masqueD.Columns.Count 
            For Each shapeTemp In ActiveSheet.Shapes
                If Not Intersect(masqueD.Cells(row, col), shapeTemp.TopLeftCell) Is Nothing Then
                        shapeTemp.TextFrame.Characters.Text = cpt 
                        cpt = cpt + 1
                End If
            Next shapeTemp
        Next col
    Next row

End Sub

我尝试shapeTemp.width在 If 中使用 as 条件。我想要做的是如果 shapeTemp 的宽度低于12.97016然后设置shapeTemp.TextFrame.Characters.Font = 8

我希望我清楚地解释了我的问题。

标签: excelvba

解决方案


反而,

为什么不创建另一个变量并将值设置为 ShapeTemp.Width 同样就像 jeeped 评论的那样,您需要实际声明 shapeTemp。

Dim shapeTemp as Shape
Dim shapeWidth as Double

shapeWidth = shapeTemp.Width

If shapeWidth = 12.907016 then
   shapeTemp.TextFrame.Characters.Font = 8
End If

推荐阅读