Hiding Columns with a Macro

beanie3105

New Member
Joined
Dec 21, 2016
Messages
18
Hello

I am currently creating management account spreadsheets for next year, I would like to show rolling monthly trends, but then hide the irrelevant sheets.

For example, in period 1 starts in column E and 12 is column P, so when it's period 1 I would want F-P hidden, when it's period 2 I want G-P hidden and so on.

Is there a way to do this with a macro which pulls from the period cell (B4)and automatically hides the columns I don't need

Thanks in advance
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
try this

Code:
Sub Current()
    Columns("E:P").EntireColumn.Hidden = True
    Columns(Application.Match(Range("B1"), Range("E1:P1"), 0) + 4).EntireColumn.Hidden = False
End Sub
 
Upvote 0
just realised that this is properly what you're after

Code:
Sub Current()
    Range("E:P").EntireColumn.Hidden = True
    Range(Columns(5), Columns(Application.Match(Range("B1"), Range("E1:P1"), 0) + 4)).EntireColumn.Hidden = False
End Sub
 
Upvote 0
Try this:

Code:
Sub Hide_Me()
Dim ans As Long
Columns.Hidden = False
ans = Range("B4").Value + 5
ActiveSheet.Range(Cells(1, ans), Cells(1, 16)).EntireColumn.Hidden = True
End Sub

OK
 
Upvote 0
Hello

That has worked! Thank you so much! I don't suppose I could ask an extra favour - do you know if it's possible to get it to run automatically when the period changes at all instead of having to run the macro?

Thank you!!
 
Upvote 0
There are more then one script provided. Which one do you want to use?
Hello

That has worked! Thank you so much! I don't suppose I could ask an extra favour - do you know if it's possible to get it to run automatically when the period changes at all instead of having to run the macro?

Thank you!!
 
Upvote 0
try this

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Range("E:P").EntireColumn.Hidden = True
    Range(Columns(5), Columns(Application.Match(Range("B1"), Range("E1:P1"), 0) + 4)).EntireColumn.Hidden = False
End Sub
 
Upvote 0
Try this:

This script will run when you enter a value into Range "B4"

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window



Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B4")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim ans As Long
ActiveSheet.Range(Cells(1, 5), Cells(1, 16)).EntireColumn.Hidden = False
If Application.WorksheetFunction.IsNumber(Range("B4")) Then
ans = Range("B4").Value + 5
ActiveSheet.Range(Cells(1, ans), Cells(1, 16)).EntireColumn.Hidden = True
Else
MsgBox "You must enter a proper number"
End If
End If
End Sub

OK
 
Upvote 0
Hello

The below works when I run it, however the latest post (by AlanY) doesn't work, when I update the macro and come out it disappears? Not sure if I am doing something wrong. The below is good, but would be great if it could do it automatically without me having to run it.

Thanks

Sub Current()
Range("E:P").EntireColumn.Hidden = True
Range(Columns(5), Columns(Application.Match(Range("B1"), Range("E1:P1"), 0) + 4)).EntireColumn.Hidden = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

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