VBA – 检查命名的范围是否隐藏。 如果不隐藏,AutoFit单元格行高

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

如果单元格可见,则需要基于行高的AutoFit例程。

我已经能够得到这个工作与静态范围,但似乎无法使用命名的范围工作。

我已经构build了这个例程来运行两个嵌套循环:一个向下看行,另一个嵌套循环向右看列。

我得到的错误是: "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已经有必要的工作表信息。 该错误是由于工作表(“owssvr”)没有称为rowcurrange的方法。

命名的范围方法实际上是这样的。 你有一个范围对象。

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