我不想问这个,因为我确实有一个解决方法,但我更喜欢一个更清晰的答案。Excel VBA:隐藏所有列,然后取消隐藏某些列
我使用的是Excel 2010,我有一个在新工作表上执行一些基本格式设置的过程:隐藏所有列,设置标题行文本,设置标题行格式,取消隐藏标题行使用的列。问题是取消隐藏不起作用。该过程运行后,工作表看起来像所有的列仍然隐藏,但如果我调整公式栏,程序unhid列出现我所期望的列。即使在保存,关闭并重新打开工作簿时,只有在调整公式栏的大小时才显示这些列。
我试着用DoEvents
来刷新屏幕。尽管我从未将它设置为false,但我试图将Application.ScreenUpdating
设置为true。我甚至试图通过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
这有一个微软的bug环。 – theMayer 2015-01-10 16:49:49