I have a list of data (text) that is sorted with bold headlines like the example below. I am looking for a way to hide/unhide rows under the headline, if possible by clicking on the cell.
**Headline 1**
Test
Test
Test
**Headline 2**
Test
Test
**Headline 3**
Test
Test
Test
Started of with this, but can't find a way to make it work (I'm new to VBA)
Sub SortBold()
Dim Rng As Range
Dim WorkRng As Range
Dim OutRng As Variant
On Error Resume Next
Set WorkRng = Sheets("Saftey functions").Range("A3:A20")
For Each Rng In WorkRng
If Rng.Font.Bold Then
If OutRng Is Nothing Then
Set OutRng = Rng
Else
Set OutRng = Union(OutRng, Rng)
End If
End If
Next
If Not OutRng Is Nothing Then
OutRng.Select
End If
Dim i As Integer
For i = 1 To UBound(OutRng)
If Not OutRng(i) Is Nothing And Not OutRng(i + 1) Is Nothing Then _
Rows(OutRng(i).Row & ":" & OutRng(i + 1)).Hidden = _
Not Rows(OutRng(i).Row & ":" & OutRng(i + 1)).Hidden
Next i
End Sub
This works by double-clicking anywhere on a row containing bold font in column A.
Put this in your worksheet's private code sheet (right-click worksheet name tab, View Code).
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Debug.Print Target.Address(0, 0)
If Cells(Target.Row, "A").Font.Bold Then
Cancel = True
On Error GoTo safe_exit
Application.EnableEvents = False
Dim rs As Long, re As Long, rng As Range
rs = Target.Row + 1
Application.FindFormat.Font.FontStyle = "Bold"
Set rng = Cells.Find(What:="*", After:=Cells(Target.Row, "A"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
SearchFormat:=True)
If rng.Row < rs Then
re = Application.Match("zzz", Columns(1)) + 1
Else
re = rng.Row
End If
Cells(rs, "A").Resize(re - rs, 1).EntireRow.Hidden = Not Cells(rs, "A").EntireRow.Hidden
End If
safe_exit:
Application.EnableEvents = True
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.