更改工作簿中所有工作表的放置属性

时间:2017-05-10 05:59:17

标签: excel-vba vba excel

我正在尝试更改工作簿中所有ChartObjects和形状的展示位置属性。

但是我收到运行时错误

  

请求的形状被锁定以供选择

在这一行:

 cht.Select

请在下面找到代码:

Sub LoopThroughCharts()
'PURPOSE: Loop through every object in the active workbook

Dim sht As Worksheet
Dim CurrentSheet As Worksheet
Dim cht As ChartObject
Dim shp As Shape

Application.ScreenUpdating = False
Application.EnableEvents = False

Set CurrentSheet = ActiveSheet
'Retrieve Input from User
PropertyOption = Application.InputBox("Change Everything To What Placement Property?" & _
"(Must be 1, 2, or 3)" & vbCr & vbCr & "   [1] Move and Size with Cells" & vbCr & _
"   [2] Move but Don't Size with Cells" & vbCr & "   [3] Don't Move or Size with Cells" & _
vbCr & " ", Type:=1, Title:="Placement Property For All")

'Handle If User Cancels
If PropertyOption = 0 Then Exit Sub

For Each sht In ActiveWorkbook.Worksheets
    For Each cht In sht.ChartObjects
        If cht.Visible = True Then
            cht.Select
            cht.Placement = PropertyOption
            'Do something with the chart...
        End If
    Next cht
Next sht

For Each sht In ActiveWorkbook.Worksheets
    For Each shp In sht.Shapes
        If shp.Visible = True Then
            shp.Select
            shp.Placement = PropertyOption
        End If
    Next shp
Next sht

Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:1)

您不需要Select Cht来修改它的属性。

几乎从不需要SelectActivate来修改属性或值,唯一的想法是#34;它确实"你的代码的运行时间很慢。

只需使用:

For Each Sht In ActiveWorkbook.Worksheets
    For Each Cht In Sht.ChartObjects
        If Cht.Visible = True Then
            Cht.Placement = PropertyOption
            'Do something with the chart...
        End If
    Next Cht
Next Sht