1
votes

I hesitate to ask this because I do have a workaround, but I'd prefer a cleaner answer.

I am using Excel 2010 and I have a procedure that does some basic formatting on a new sheet: hides all columns, sets the header row text, formats the header row, unhides the columns the header row uses. The problem is that the unhide doesn't quite work. After the procedure is run, the worksheet looks like all of the columns are still hidden, but if I resize the formula bar, the columns that the procedure unhid appear as I would expect. Even when the workbook is saved, closed, and reopened the columns don't appear until I resize the formula bar.

I tried using DoEvents to refresh the screen. I tried setting Application.ScreenUpdating to true even though I was never setting it to false. I even tried hiding and unhiding the formula bar through VBA. The only thing that works (my workaround) is to resize the formula bar as part of the procedure. It does work, but seems like it shouldn't be necessary. It might work to activate the range before I unhide it, but I prefer not to use Activate or Select in VBA.

Any thoughts?

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
3
This has the ring of a Microsoft bug. – theMayer

3 Answers

0
votes

LastCol = Range("A1").End(xlToRight).Column

With sh

.Cells(1, EndCol + 1).Resize(, LastCol - EndCol).Columns.Hidden = True

End With

1
votes

If you want it to unhide all cells:

cells.EntireColumn.Hidden = False

And if you only want to unhide the 5 columns that are used in the header, then:

Range(Cells(1, 1), Cells(1, EndCol)).EntireColumn.Select

This will only unhide the columns that are within the "Header", and it has to be put outside of the With statement to work (put it as the last line). It uses .select, I know, but thats the only way I can get it to work....

1
votes

The following will hide all the columns then selectively unhide.

worksheet.Cells.EntireColumn.Hidden = true
worksheet.Cells(1,1).EntireColumn.Hidden = false
worksheet.Cells(1,2).EntireColumn.Hidden = false

note This only works with columns

worksheet.Cells.EntireRow.Hidden = true

Does not work.