Mohsen
Mohsen

Reputation: 23

Excel VBA filter merged cell

I have Excel sheet with 5 variables : Exam, Months (January, February, March, April). every month has 3 variables: Low, Medium, High. And I want VBA code to do filter to shows only column "High" for all months. My code works fine, but when I make months merged cell it doesn't work:

Sub filter_high()

Columns("B:C").Select
Selection.EntireColumn.Hidden = True
Columns("E:F").Select
Selection.EntireColumn.Hidden = True
Columns("H:I").Select
Selection.EntireColumn.Hidden = True
Columns("K:L").Select
Selection.EntireColumn.Hidden = True

End Sub

enter image description here

Upvotes: 1

Views: 811

Answers (1)

Tomasz
Tomasz

Reputation: 426

you can set width of column, efect same like visible off

Option Explicit

Sub showMediumOnly()
    Dim arr(2) As String
    Dim c As Integer
    Dim item As Variant
    
    arr(0) = "Low"
    arr(1) = "High"
    
    For c = 1 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column + 2  '  loop trought columns +2 cuz merget columns
        For Each item In arr    '   checking array
            If ActiveSheet.Cells(2, c) = item And ActiveSheet.Cells(2, c) <> "" Then ActiveSheet.Columns(c).ColumnWidth = 0 '   condition to check if its target array and change width
        Next
    Next
End Sub

Upvotes: 0

Related Questions