vba to Hide Columns based on User Input

The_Rock

Board Regular
Joined
Jul 2, 2007
Messages
174
Hi Folks
Can you help me with the following.

In Column B, I have a list of countries.
Cols C2:BB2, I have week number 1 to 52 (for 2013)
Col BC is the Total for 2013.

Cols BD2:DC2 are week numbers for 2014, again, numbered 1 to 52.
DD is the total for 2014

What I would like is to have a macro that when it is run, it asked the user which weeks they would like to view.
For example, if the user enters 3, it would then hide columns F:BB (for 2013) and Columns BG:DC (for 2014) - this would leave weeks 1 to 3 visible

If the user entered 10, it would hide columns M:BB (for 2013) and Columns BN:DC (for 2014) - this would leave weeks 1 to 10 visible.

Cheers!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try:
Code:
Sub HideCol()
    Application.ScreenUpdating = False
    Cells.EntireColumn.Hidden = False
    Dim weekNum As String
    weekNum = InputBox("Please enter the number of weeks to view.")
    Range(Cells(1, weekNum + 3), Cells(1, "BB")).EntireColumn.Hidden = True
    Range(Cells(1, 56 + weekNum), Cells(1, "DC")).EntireColumn.Hidden = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

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