为什么有时需要选择对象以避免"对象不支持此属性或方法"

时间:2016-04-28 13:57:38

标签: excel vba excel-vba

我注意到我有时会收到错误:

  

运行时错误'''

     

对象不支持此属性或方法

在Excel VBA中

但是如果我先在对象上调用.select,那么此错误就会消失。

例如,我最近想要更改工作表上某些链接图像的大小,并使用宏录制器录制了以下内容:

ActiveSheet.Shapes.Range(Array("Picture 3")).Select
Selection.ShapeRange.Height = 303.12

这段代码显然工作正常。然后我按如下方式更改了代码:

Dim sheetReport as Worksheet
Set sheetReport = Worksheets("Report")

With sheetReport
    Dim pictureNumber As Long
    For pictureNumber 1 to 3
        .Shapes.Range("Picture " & pictureNumber).ShapeRange.Height = 303.12
    Next pictureNumber
End With

现在我收到上面提到的错误如果我然后将此代码更改为

Dim sheetReport as Worksheet
Set sheetReport = Worksheets("Report")

With sheetReport
    Dim pictureNumber As Long
    For pictureNumber 1 to 3
        .Shapes.Range("Picture " & pictureNumber).Select
        Selection.ShapeRange.Height = 303.12
    Next pictureNumber
End With

再次有效。但我当然不应该只选择每张图片以改变其Height属性吗?

1 个答案:

答案 0 :(得分:4)

这在我的测试运行中运行良好。

   SELECT MAX(sum_total_pay)
FROM
(
    SELECT SUM(ALLOCATION.HourlyRate *  ACTION.HrsWorked) AS sum_total_pay
    FROM ALLOCATION 
    INNER JOIN ACTION 
    ON ((ACTION.ActId  = ALLOCATION.ActId) AND (ACTION.EmpId  = ALLOCATION.EmpId))
    GROUP BY (ALLOCATION.ActId)
);