VBA macro to dynamically show range of columns

Mr_Peter

New Member
Joined
Nov 14, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
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:

1669098832040.png


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.

1669099084882.png


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!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
will hide all columns that have dates less than B1 would work. if so try

VBA Code:
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
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,428
Members
448,896
Latest member
MadMarty

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
Back
Top