Hide/Unhide Columns using VBA

hq_life

New Member
Joined
Sep 12, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello All,

First post. New to VBA so really confused on what to do next.

What I am trying to achieve:
In Sheet 1, I have a drop down from "1 to 11"
If someone selects 1, I want to hide Columns D:M in Sheet 13. If someone selects 2, I want to hide columns E:M in Sheet 13, and so on.

This is the code I have so far:

If [B9] = "1" Then
Sheet13.Columns("D:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("D:M").EntireColumn.Hidden = False
End If

If [B9] = "2" Then
Sheet13.Columns("E:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("E:M").EntireColumn.Hidden = False
End If

This code works if I select "2", but when I select "1" it ONLY hides column D, and not the remaining E:M. I also want to extend this code if I select 3 (Hide F:M), 4 (Hide G:M), etc...
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Board!

I think you have conflicting information.
For example, if you choose 2, the "Else" portion of the first IF is telling it to NOT hide columns D:M, but the second IF is telling hide columns E:M.

In these situations, I think it is best to usually start of unhiding everything, and then just hiding what you need.
And I would recommend using a "Case" statement insteaqd of a whole bunch of IF/THEN/ELSE statements, i.e.
VBA Code:
Sheet13.Columns("D:M").EntireColumn.Hidden = False

Select Case Range("B9")
    Case 1
        Sheet13.Columns("D:M").EntireColumn.Hidden = True
    Case 2
        Sheet13.Columns("E:M").EntireColumn.Hidden = True
End Select
 
Last edited:
Upvote 0
Each If block will run because you don't Exit Sub when the block is executed. Try adding that before each End If. However, it's not clear what you want to happen if I first choose 1, then I choose 2. What's supposed to happen to the hidden columns? If unhide, then probably best to first unhide all then hide according to the choice.
Please post code within code tags (use vba button on posting toolbar) to maintain indentation and readability.
 
Upvote 0
Late again. But the unhide would be D:M not E:M?
 
Upvote 0
Hello All,

First post. New to VBA so really confused on what to do next.

What I am trying to achieve:
In Sheet 1, I have a drop down from "1 to 11"
If someone selects 1, I want to hide Columns D:M in Sheet 13. If someone selects 2, I want to hide columns E:M in Sheet 13, and so on.

This is the code I have so far:

If [B9] = "1" Then
Sheet13.Columns("D:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("D:M").EntireColumn.Hidden = False
End If

If [B9] = "2" Then
Sheet13.Columns("E:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("E:M").EntireColumn.Hidden = False
End If

This code works if I select "2", but when I select "1" it ONLY hides column D, and not the remaining E:M. I also want to extend this code if I select 3 (Hide F:M), 4 (Hide G:M), etc...

Welcome to Mr Excel.

Does this give you what you need?

Put this code into the worksheet code module of the worksheet with the drop down validation list.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim arrColumns() As String
Dim Ws As Worksheet

    If Target.CountLarge > 1 Then
        Exit Sub
    End If

    Set Ws = Worksheets("Sheet13")
    
    arrColumns = Split("D:M,E:M,F:M,G:M,H:M,I:M,J:M,K:M,L:M,M:M", ",")
        
    If Not Intersect(Target, Range("B9")) Is Nothing Then

        Ws.Range("D:M").EntireColumn.Hidden = False
        
        Ws.Range(arrColumns(Target.Value - 1)).EntireColumn.Hidden = True
    
    End If

End Sub
 
Upvote 0
Sorry - I am very new to VBA.
Would this code be in addition to the IF statement? Something like the below? Very sorry!
VBA Code:
If [B9] = "1" Then
Sheet13.Columns("D:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("D:M").EntireColumn.Hidden = False
Select Case Range("B9")
    Case 1
        Sheet13.Columns("D:M").EntireColumn.Hidden = True
    Case 2
        Sheet13.Columns("E:M").EntireColumn.Hidden = True
End Select
End If
 
Upvote 0
Sorry - I am very new to VBA.
Would this code be in addition to the IF statement? Something like the below? Very sorry!
VBA Code:
If [B9] = "1" Then
Sheet13.Columns("D:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("D:M").EntireColumn.Hidden = False
Select Case Range("B9")
    Case 1
        Sheet13.Columns("D:M").EntireColumn.Hidden = True
    Case 2
        Sheet13.Columns("E:M").EntireColumn.Hidden = True
End Select
End If
I'm not sure where your code is.

This takes the place of any other code you have to hide and hide and unhide these columns.

It is automaticlly triggered when the value in B9 changes when a different value is selected from the drop down list.
 
Upvote 0
Sorry - I am very new to VBA.
Would this code be in addition to the IF statement? Something like the below? Very sorry!
VBA Code:
If [B9] = "1" Then
Sheet13.Columns("D:M").EntireColumn.Hidden = True
Else
Sheet13.Columns("D:M").EntireColumn.Hidden = False
Select Case Range("B9")
    Case 1
        Sheet13.Columns("D:M").EntireColumn.Hidden = True
    Case 2
        Sheet13.Columns("E:M").EntireColumn.Hidden = True
End Select
End If
No, my code replaces ALL of that.

Note that if the value in cell B9 is a valid numeric entry and not text, you do not need the double-quotes around it (they are used for text entries, not numeric ones).
That is why I used:
VBA Code:
Case 1
instead of
VBA Code:
Case "1"
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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