为什么在 Excel VBA 中通过形状循环给出 1004 错误?

问题描述 投票:0回答:0

我编写了以下程序,通过单击自定义选项卡上的自定义图标运行。

程序应该:

  • 找到最接近形状的(TopLeft)单元格的行和列
  • 将找到的单元格的行高设置为比形状高度大 10 个单位

代码如下:


Sub AdjustRowHeightToCode(control As IRibbonControl)
'Sub AdjustRowHeightToCode()
'A procedure that sets height of a merged cell in which shape is located
    Dim wsh As Worksheet
    Dim i As Integer
    Dim col_n As Long
    Dim sh As Shape
    Dim sh_cell As Range
        
    Set wsh = ThisWorkbook.Worksheets("Wyniki")
        
    For Each sh In wsh.Shapes
        Set sh_cell = sh.TopLeftCell
        col_n = sh_cell.Column

        If col_n >= 2 And col_n <= 5 Then
        'We want to set height of cells from columns B to E
            sh.Select
            sh_cell.RowHeight = sh.Height + 10
        End If
    Next sh
End Sub

当我通过单击图标运行程序时,我得到

运行时错误“1004”: 应用程序定义或对象定义的错误

For
循环中的调试器停止并且似乎没有检测到“sh.TopLeftCell”属性,如下面的屏幕所示。

相反,它能够检测

Colum
甚至
Row
属性
sh_cell

我还是不明白为什么调试器会在此时停止? 你有什么线索吗?

excel vba controls shapes
© www.soinside.com 2019 - 2024. All rights reserved.