VBA - 检查命名范围是否被隐藏。如果未隐藏,AutoFit 单元格行高

VBA - Check if named range is hidden. If not hidden, AutoFit cell Row height

我有一个绑定到 SharePoint 列表的动态 table。在任何给定时间,对于电子表格上生成的报告,只有 1 列可见。由于 table 可能会增大或缩小,我需要一个例程来调整任意数量的行,然后查看每一行以确定该行的单元格是否可见。

如果单元格可见,我需要根据行高自动调整例程。

我已经能够让它与静态范围一起工作,但似乎无法让它与命名范围一起工作。

我已经将例程构建为 运行 两个嵌套循环:一个向下查看行,第二个嵌套循环向右查看列。

我得到的错误是:"Run-Time error '438': Object doesn't support this property or method".

这里出现错误:

If Worksheets("owssvr").rowcurrange.EntireColumn.Hidden = False Then

非常感谢任何帮助!

谢谢!

Public Sub AutoFit()
'Sub to autofit the contents of the desired field in the report.
'The routine loops through the table to determine the visible cells, and if visible, autofits the contents

Dim lastrow As Long 'lastrow of table
Dim rowNumber As Long 'counter to determine current visible row number of table
Dim columnrange As Range 'full column range of table
Dim cell As Range 'range used to check if row is visible
Dim rowrange As Range 'loops through row range to determine if current cell is hidden, if so autofit
Dim rowcurrange As Range 'current cell of rowrange

rowNumber = 4
lastrow = Worksheets("owssvr").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set columnrange = Worksheets("owssvr").Range("A" & rowNumber & ":A" & lastrow) 'sets range to check autofit

For Each cell In columnrange 'loops through and checks to see if current row is visible

    Set rowrange = Worksheets("owssvr").Range("G" & rowNumber & ":AR" & rowNumber) 'set current row to dnyamically autofit

    For Each rowcurrange In rowrange 'loops through current row to check if row is hidden, if visible autofit rowcurrange
    MsgBox (rowcurrange)
    If Worksheets("owssvr").rowcurrange.EntireColumn.Hidden = False Then

        Worksheets("owssvr").rowcurrange.Rows.AutoFit 'autofits only the field cell
        MsgBox (rowcurrange)

    End If
    Next

    rowNumber = rowNumber + 1
Next

End Sub

将虚线改为

If rowcurrange.EntireColumn.Hidden = False Then

您超出了指定范围。 rowcurrange 已经具有必要的工作表信息。该错误是由于 Worksheets("owssvr") 没有名为 rowcurrange 的方法。

命名范围方法实际上看起来像这样。你有一个范围对象。

If Worksheets("owssvr").range("MyNamedRange").EntireColumn.Hidden = False` Then