Hide or show rows and columns in Excel with VBA
A frequently asked question is how to hide rows and/or columns in Excel using a VBA macro.
On this page are examples on how to do it. To test the code, highlight it with the mouse, copy (CTRL+C) and paste (CTRL+V) into a VBA module.
You can also download a zip-compressed spreadsheet with the examples. To unzip, right-click and select whatever Windows suggests to unzip the archive.
Basically you can use two different methods. You can do it "automatically" by putting code in the worksheet's code sheet. The WorkSheet_Change procedure is activated every time something is changed in the worksheet.
The other approach is to run an "ordinary" macro that checks if certain conditions are met. Let us start by using the last method.
The first example shows how to hide empty columns, and the next example shows how to use the search function and hide rows, if a certain value exists in a given range.
However before we start playing hide and seek, I want to show you how to make the hidden rows and columns visible again!
Sub ShowHidden()
Rows.Hidden = False
Columns.Hidden = False
End Sub
Hide empty columns
This is a pretty simple example on how to hide empty columns in a range - here column 1 to 26 (A to Z).
Sub HideEmptyColumns()
Dim rCell As Range
Dim rTest As Range
Dim lCol As Long
On Error GoTo ErrorHandle
Application.ScreenUpdating = False
For lCol = 0 To 25 Step 1
Set rCell = Range("A1").Offset(0, lCol)
If Len(rCell.Value) = 0 Then
Set rTest = rCell.End(xlDown)
If rTest.Row = Rows.Count And Len(rTest.Value) = 0 Then
Columns(rCell.Column).Hidden = True
End If
End If
Next
BeforeExit:
Set rCell = Nothing
Set rTest = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Procedure HideEmptyColumns"
Resume BeforeExit
End Sub
Using the search function
The following example uses Excel's search function in a given range - here column B, but that can easily be changed.
We ask the user what to search for, and a row is hidden if it has the seach value in column B. Note that "?" and "*" can be used as wildcards or "jokers".
Sub FindHide()
Dim vFind
Dim rSearch As Range
On Error GoTo ErrorHandle
vFind = InputBox("What to search for?")
If Len(vFind) = 0 Then Exit Sub
Application.ScreenUpdating = False
With Columns("B:B")
Set rSearch = .Find(vFind, LookIn:=xlValues)
If Not rSearch Is Nothing Then
rSearch.EntireRow.Hidden = True
Do
Set rSearch = .FindNext(rSearch)
If Not rSearch Is Nothing Then
rSearch.EntireRow.Hidden = True
Else
Exit Do
End If
Loop
End If
End With
BeforeExit:
Set rSearch = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Procedure FindHide"
Resume BeforeExit
End Sub
Automatic code
The next two macros are examples of what I call "automatic code," because they execute automatically, if a certain condition is met. So you don't need to do anything active like clicking a button or the like to run the code.
You can do that by putting VBA code into the worksheet's own code sheet. Then events can execute a macro automatically.
We use the standard procedure "Worksheet_Change", which is called automatically when something changes in the worksheet. If you cannot see the worksheets code sheet in the VBA editor press CTRL+R to open Project Explorer:
The following Worksheet_Change procedures must be in the worksheet's code sheet to work.
The first example is very simple: If you write an "x" in column A, the row with the "x" will be hidden. If you write a 0 (zero) all hidden rows will be visible again, and old values in column A are deleted.
In the last example you can type something in cell A1, and all rows (except row 1) that have the same value in column A will be hidden.
It is not unlike the example, where we used the search function, but here we loop through the cells instead.
Hide rows with an x
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
With Target
If .Column <> 1 Then
Exit Sub
Else
If .Value = "x" Then
.EntireRow.Hidden = True
End If
If .Value = 0 And Len(.Value) > 0 Then
Application.EnableEvents = False
Rows.Hidden = False
Columns(1).Clear
Application.EnableEvents = True
End If
End If
End With
End Sub
Hide rows using criteria
The last example shows how to hide all rows that have the same value in column A as in cell A1.
For that matter the whole thing could be written in the worksheet's Worksheet_Change procedure, but for the sake of the example I have made the Worksheet_Change procedure call an "ordinary" macro to do some of the work.
So you must put the Worksheet_Change procedure in the worksheet's code sheet and the procedure HideUsingCriteria in a standard module.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrorHandle
If Target <> Range("A1") Then
Exit Sub
Else
Application.ScreenUpdating = False
If Target.Value = 0 Then
ActiveSheet.Rows.Hidden = False
ElseIf Len(Target.Value) > 0 Then
Application.EnableEvents = False
HideUsingCriteria Target.Value
Application.EnableEvents = True
End If
End If
BeforeExit:
Application.ScreenUpdating = True
Range("A1").Activate
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Procedure Worksheet_Change"
Resume BeforeExit
End Sub
Insert the following procedure in a standard VBA module.
Sub HideUsingCriteria(ByVal sXY As String)
Dim rCell As Range
Dim rRange As Range
On Error GoTo ErrorHandle
Set rCell = Range("A" & Rows.Count)
If rCell.Value = sXY Then
rCell.EntireRow.Hidden = True
End If
Set rCell = rCell.End(xlUp)
If rCell.Row = 1 Then GoTo BeforeExit
Set rRange = Range(rCell, Range("A2"))
For Each rCell In rRange
If rCell.Value = sXY Then
rCell.EntireRow.Hidden = True
End If
Next
BeforeExit:
Set rCell = Nothing
Set rRange = Nothing
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Procedure HideUsingCriteria"
Resume BeforeExit
End Sub
That was it. As mentioned you can download a zipped spreadsheet with the examples.
Related:
|