How to automatically hide/unhide columns based on related month with VBA

ysfuyar

New Member
Joined
Jun 13, 2024
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Dears

I want create auto working monthly comparative charts as u know, to create a proper chart i have to hide unrealized months, for example i am working on Jan2June chart for now after June values are 0 but chart still shows after June months but it is not necessary. I want to auto hide columns with VBA i have very simple code but it has some crash when i add more criteria. If i only define 6. month (June) the code works fine hides after June months. But ,if i add 7. month in code it works for July when i write 7 in to trigger cell but when i write 6 to trigger cell it only hide July and keeps other columns (months) unhide. Where is the bug ? Thanks in advance.

If Range("P1").Value = 6 Then
Columns("I:N").EntireColumn.Hidden = True
Columns("X:AC").EntireColumn.Hidden = True
Else
Columns("I:N").EntireColumn.Hidden = False
Columns("X:AC").EntireColumn.Hidden = False
End If
If Range("P1").Value = 7 Then
Columns("J:N").EntireColumn.Hidden = True
Columns("Y:AC").EntireColumn.Hidden = True
Else
Columns("J:N").EntireColumn.Hidden = False
Columns("Y:AC").EntireColumn.Hidden = False
End If
End Sub

1721823524609.png
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
GPT 4 gave me the code

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("P1")) Is Nothing Then
Call AySonrasiKolonlariGizle
End If
End Sub

Sub AySonrasiKolonlariGizle()
Dim sonAy As Integer

sonAy = Range("P1").Value

Columns("I:AC").EntireColumn.Hidden = False

Select Case sonAy
Case 6
Columns("I:N").EntireColumn.Hidden = True
Columns("X:AC").EntireColumn.Hidden = True
Case 7
Columns("J:N").EntireColumn.Hidden = True
Columns("Y:AC").EntireColumn.Hidden = True
End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,165
Members
452,615
Latest member
bogeys2birdies

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