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

时间:2015-03-27 18:39:53

标签: excel vba excel-vba excel-2013

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

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

我已经能够使用静态范围,但似乎无法使用命名范围。

我已经构建了运行两个嵌套循环的例程:一个用于向下查看行,另一个嵌套循环用于查看列。

我得到的错误是:"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

1 个答案:

答案 0 :(得分:2)

将您的折线更改为

If rowcurrange.EntireColumn.Hidden = False Then

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

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

If Worksheets("owssvr").range("MyNamedRange").EntireColumn.Hidden = False` Then
相关问题