# VBA macro to dynamically show range of columns



## Mr_Peter (Nov 22, 2022)

Hi excel experts, I have a tricky VBA macro I am trying to write and I'm hoping someone will be able to help me out. If I have a date value in cell "B1", how would I go about writing the macro to show or copy 13 columns with the first column header matching the date value in "B1", then showing the next 12 columns after that and hiding the rest. For a better explanation of what I'm trying to achieve, I am attaching some pictures below of what the end goal is:






In the picture above my target date in "B1" is 2022/11/13, so I would like the macro to check the column headers from "C" to "S", match where the column header in row 3 matches 2022/11/13, then take the next 12 columns afterwards that have the *Planned* header above the date cell and ignoring the Actuals columns. So the end goal of this macro would be the next picture below.





From what I understand, I need an if statement to check the condition for a match between "B1" and the range of columns, and if the condition is matched then loop the range of columns using a do until loop that stops at 13, with the column headers being planned as well. I'm just not sure how to write this in VBA code, please will somebody help me. I'm a bit stuck as I'm quite new to VBA, thank you!


----------



## hajiali (Dec 17, 2022)

will hide all columns that have dates less than B1 would work. if so try 


```
Sub hidecolumns()
Dim Cl As Long

For x = 3 To 19
    If Cells(3, x) < Range("B1") Then
            Columns(x).EntireColumn.Hidden = True
    End If
Next
End Sub
```

not code above will only check columns C through S let me know if that's what your needing


----------

