【发布时间】:2015-01-09 18:15:41
【问题描述】:
我不愿问这个问题,因为我确实有一个解决方法,但我更喜欢更简洁的答案。
我正在使用 Excel 2010,并且我有一个在新工作表上执行一些基本格式设置的程序:隐藏所有列、设置标题行文本、格式化标题行、取消隐藏标题行使用的列。问题是取消隐藏不太有效。运行该过程后,工作表看起来所有列仍处于隐藏状态,但如果我调整编辑栏的大小,该过程未隐藏的列会按预期显示。即使保存、关闭和重新打开工作簿,在我调整编辑栏大小之前,这些列也不会出现。
我尝试使用DoEvents 刷新屏幕。我尝试将 Application.ScreenUpdating 设置为 true,即使我从未将其设置为 false。我什至尝试通过 VBA 隐藏和取消隐藏公式栏。唯一有效的方法(我的解决方法)是在过程中调整公式栏的大小。它确实有效,但似乎没有必要。在我取消隐藏之前激活范围可能会起作用,但我不喜欢在 VBA 中使用 Activate 或 Select。
有什么想法吗?
Private Sub FormatSheet(sh As Worksheet)
Dim HeaderText As Variant
Dim EndCol As Long
Dim Header As Range
'header items for sheet
HeaderText = Array("DATE", "USER", "BC", "TC", "SUM")
'get last column index based on headers
EndCol = UBound(HeaderText) - LBound(HeaderText) + 1
With sh
'hide all columns in the sheet
.Columns.Hidden = True
'set the header range
Set Header = .Range(.Cells(2, 1), .Cells(2, EndCol))
'set the header text
Header = HeaderText
'set the header row formatting
With .Rows(2)
.Font.Bold = True
.Interior.Color = RGB(217, 217, 217)
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
End With
'unhide the columns used by the header
Header.EntireColumn.Hidden = False
'resize the formula bar to force the unhide to work
Application.FormulaBarHeight = 5
Application.FormulaBarHeight = 1
'autofit columns
.Columns.AutoFit
End With
End Sub
【问题讨论】:
-
这有微软漏洞的影响。