我可以在VBA中使用shape.width作为条件吗

时间:2018-10-12 14:23:00

标签: excel vba

我想在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中的条件。我想做的是,如果shapeTemp的宽度低于 12.97016 ,然后设置shapeTemp.TextFrame.Characters.Font = 8

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

2 个答案:

答案 0 :(得分:0)

相反,

为什么不创建另一个变量并将其值设置为ShapeTemp.Width 就像吉普车评论一样,您实际上需要声明shapeTemp。

Dim shapeTemp as Shape
Dim shapeWidth as Double

shapeWidth = shapeTemp.Width

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

答案 1 :(得分:0)

您声明了局部变量[ {"5b3f77502dfe0deeb8912b42", [ {"5b71d7e458c37fa04a7ce768", "5b3f77502dfe0deeb8912b42", "1538077790705827"}, {"5b71d7e458c37fa04a7ce768", "5b3f77502dfe0deeb8912b42", "1538078530667847"}, {"5b71d7e458c37fa04a7ce768", "5b3f77502dfe0deeb8912b42", "1538077778390908"} ]}, {"5bad45b1e990057961313822", [ {"5b71d7e458c37fa04a7ce768", "5bad45b1e990057961313822", "1538082492283531"} ]} ] ,但未将其设置为任何值。您需要将shapeTemp设置为某些值。这是有关VBA中变量范围的更多信息:

https://support.microsoft.com/en-us/help/141693/scope-of-variables-in-visual-basic-for-applications

下面有一些如何设置shapeTemp的示例。

要创建新形状,请执行以下操作:

shapeTemp

要检索shapes集合中已经存在的第一个形状:

Set shpTemp = ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 10, 10, 100, 100)

您还可以将shpTemp设置为另一个现有形状,或者可以使用参数/参数将形状对象传递到过程中。