Hi,

This is the original sub I wrote for each toggle button and now I want to turn it into a function to make my code cleaner

'Dim sht As Excel.Worksheet
Dim c As Integer
Dim c2 As Integer

'Set sheet19 = Sheet19
c2 = 32000

Application.ScreenUpdating = False

' this hides the rows for the range
If tgbtnTablets.Value = False Then
 Debug.Print tgbtnTablets.Value
 tgbtnTablets.Caption = "Show Detail"

For c = 1 To Sheet19.Range("rg_main_tablets").Rows.Count
        If Sheet19.Range("rg_main_tablets").Cells(c, 1).Value = Sheet19.Range("rg_main_tablets").Cells(c, 2).Value = "Visible" Then
            Sheet19.Range("rg_main_tablets").Columns(c).Hidden = False
            If c < c2 Then
                c2 = c
            End If
        Else
            Sheet19.Range("rg_main_tablets").Rows(c).Hidden = True
        End If
    Next

    Application.ScreenUpdating = True
    
    Else
     Debug.Print tgbtnTablets.Value
    Application.ScreenUpdating = False

' this shows the rows for the range

For c = 1 To Sheet19.Range("rg_main_tablets").Rows.Count
        If Sheet19.Range("rg_main_tablets").Cells(c, 1).Value = Sheet19.Range("rg_main_tablets").Cells(c, 2).Value = "Visible" Then
            Sheet19.Range("rg_main_tablets").Columns(c).Hidden = True
            If c < c2 Then
                c2 = c
            End If
        Else
            Sheet19.Range("rg_main_tablets").Rows(c).Hidden = False
        End If
    Next

    Application.ScreenUpdating = True
    tgbtnTablets.Caption = "Hide Detail"
    
End If
The above works fine with no issues.

Could you advise me on how best to turn this into a function ? The original thread heading was my attempt at writing a function.

Bilal