Private Const cMAX_COL_BLOCKS = 9 [COLOR=green]' A-Z to HA-HZ (increase this number if more required)[/color]
Private Const cALPHABET = 26
Public Sub HideColIfZero(ByVal Sheet As Worksheet, _
ByVal StartColumn As Variant, _
Optional ByVal EndColumn As Variant = -1)
Dim lStartCol As Long
Dim lEndCol As Long
Dim i As Long
Dim j As Long
Dim dVal As Double
Dim blnExclude As Boolean
[COLOR=green] '-------------------------------------------------
' Column can be a letter or a number - convert to a number
'-------------------------------------------------[/color]
If IsNumeric(StartColumn) = False Then
lStartCol = ColumnLetterToNumber(StartColumn)
Else
lStartCol = StartColumn
End If
If IsNumeric(EndColumn) = False Then
lEndCol = ColumnLetterToNumber(EndColumn)
Else
If EndColumn = -1 Then
lEndCol = lStartCol
Else
lEndCol = EndColumn
End If
End If
[COLOR=green] '-------------------------------------------------
' process the columns
'-------------------------------------------------[/color]
For i = lStartCol To lEndCol
For j = lStartCol To lEndCol
dVal = dVal + Abs(Cells(i, j).Value)
Next j
If (dVal = ZERO_DOUBLE) Then
blnExclude = False
For j = LBound(Exclude) To UBound(Exclude)
If i = Exclude(j) Then
blnExclude = True
Exit For
End If
Next j
If blnExclude = False Then
Sheet.Columns(i).EntireColumn.Hidden = True
End If
End If
dVal = 0
Next i
End Sub
Public Function ColumnLetterToNumber(ByVal Letter As String) As Long
Const cA = 64 [COLOR=green]' Asc("A")[/color]
Dim lTemp As Long
Dim strLetter As String
Dim strTemp As String
Dim i As Long
On Error GoTo ErrorHandler
strLetter = UCase(Letter)
If Len(strLetter) = 1 Then
lTemp = Asc(strLetter) - cA
Else
For i = 1 To cMAX_COL_BLOCKS
strTemp = Chr(i + cA)
If StrComp(strTemp, Left(strLetter, 1), vbTextCompare) = 0 Then
lTemp = (i * cALPHABET) + Asc(Right(strLetter, 1)) - cA
Exit For
End If
Next i
End If
ColumnLetterToNumber = lTemp
Exit Function
ErrorHandler:
Err.Raise Err.Number, Err.Description
Err.Clear
End Function