How to apply VBA code (for horizontal filters) to another 'place' on the sheet?

rlvdberg

New Member
Joined
Jan 1, 2021
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
Dear all,

I am trying to make a sheet in which I can use both vertical filters (the 'normal' Excel option) and horizontal filters. Transposing the data is not an option, because I need both filters in the sheet for data entry.
I have been researching and found the following VBA code: Andrew's Excel Tips:Horizontal Filter. See below.

Since then, I have done some research on VBA code (I am an absolute beginner). I have now managed to apply the same code in a new worksheet. However, since I am not apt at 'reading' code, I don't know how to change the 'place' on the sheet where the code works/applies.

Currently the horizontal filters are set up in column B, starting from row 2. I need them to be moved to column G, starting from row 15 up to and including 25.

Can someone help me to identify how to change the code or where I can find an information resource in order to help me understand how to do this?

Thanks in advance!

VBA Code:
Option Explicit

Private c As Range
Private rLastCell As Range
Private rHFilterRow As Range
Private i As Long
Private strFilter As String
Private bFilter As Boolean
Private lCalc As Long

Sub SetrHFilterRange()

    On Error Resume Next

    Application.ScreenUpdating = False

    ' Get the Last Cell of the Used Range
    Set rLastCell = ThisWorkbook.Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell)

    ' Reset Range "rHFilter" from Cell C2 to last cell in Used Range
    ThisWorkbook.Names.Add Name:="rHFilter", RefersTo:= _
                           "=Sheet1!$C$2:" & rLastCell.Address

    For Each rHFilterRow In Range("rHFilter").Rows

        With rHFilterRow

            With Cells(.Row, 2)
                .Value = "-"
                .FormatConditions.Delete
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="=""-"""
                .FormatConditions(1).Interior.ColorIndex = 44
                .Interior.ColorIndex = 22
            End With

            strFilter = "-"

            i = 3

            ' Get the unique values in each row of rHFilter
            ' Then make a list with Data Validation
            For Each c In .Cells

                If Application.CountIf(Range(Cells(.Row, 3), _
                                             Cells(.Row, i)), c.Value) = 1 Then

                    strFilter = strFilter & "," & c.Value

                End If

                i = i + 1

            Next c

            With Cells(.Row, 2).Validation
                .Delete
                .Add Type:=xlValidateList, Formula1:=strFilter & ",Blank Cells"
                .InCellDropdown = True
            End With

            strFilter = ""

        End With

    Next rHFilterRow

    For i = 1 To 4

        Range(Cells(2, 1), rLastCell).Borders(i).LineStyle = xlContinuous

    Next i

    Application.ScreenUpdating = True

    On Error GoTo 0
End Sub

Sub SetrHFilter()

    On Error Resume Next

    ThisWorkbook.Sheets(1).Columns.Hidden = False

    If Application.CountIf(ThisWorkbook.Sheets(1).Columns(2), "-") _
       = Range("rHFilter").Rows.Count Then Exit Sub

    If rLastCell Is Nothing Then

        Set rLastCell = ThisWorkbook.Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell)

    End If

    ' Speed the code up changing the Application settings

    With Application
        lCalc = .Calculation
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With

FilterRows:

    ' Hide columns if cells don't match the values in Column B

    For Each rHFilterRow In Range("rHFilter").Rows

        With rHFilterRow

            If Cells(.Row, 2) <> "-" Then

                For Each c In Range(Cells(.Row, 3), Cells(.Row, rLastCell.Column))

                    If Cells(.Row, 2).Value = "Blank Cells" Then

                        If c.Value <> "" Then c.EntireColumn.Hidden = True

                    Else

                        If c.Value <> Cells(.Row, 2).Value Then c.EntireColumn.Hidden = True

                    End If

                Next c

            End If

        End With

    Next rHFilterRow

    If bFilter = False Then
        bFilter = True
        GoTo FilterRows
    End If

    ' Change the Application settings back

    With Application
        .Calculation = lCalc
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set rLastCell = Nothing

    On Error GoTo 0
End Sub

Sub ResetrHFilter()

    On Error Resume Next

    ThisWorkbook.Sheets(1).Columns.Hidden = False

    SetrHFilterRange

    On Error GoTo 0
End Sub
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Watch MrExcel Video

Forum statistics

Threads
1,127,099
Messages
5,622,698
Members
415,922
Latest member
gemmatay88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top