VBA code not working as expected, hide columns

ranjanagarwal

New Member
Joined
Jul 12, 2015
Messages
13
The problem I am having with the following code is that I am not able to select the values one after the other. I always have to go back to empty cell value before I can select the other value.
For example; if I select "1st' it displays the required info and hides the columns that are supposed to hidden. If I go next and select '2nd' as value it will hide all the columns instead of just hiding the required ones. However if I select an empty cell value and then select '2nd' then it works fine.

Second issue that I am having is that I am not able to name it anything else such as 'Game 1', 'Game 2' etc Is there some sort of restriction?

Thanks in advance for looking into this.
This code was from an old post on the website and amended it to suit my need.
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Cells.Count = 1 And Target.Address = "$C$3" Then
If LCase(Target.Value) = "1st" Then

Columns("F:U").EntireColumn.Hidden = True

ElseIf LCase(Target.Value) = "2nd" Then

Columns("H:U").EntireColumn.Hidden = True
Columns("D:E").EntireColumn.Hidden = True

ElseIf LCase(Target.Value) = "3rd" Then

Columns("J:U").EntireColumn.Hidden = True
Columns("D:G").EntireColumn.Hidden = True
ElseIf LCase(Target.Value) = "4th" Then

Columns("L:U").EntireColumn.Hidden = True
Columns("D:I").EntireColumn.Hidden = True
ElseIf LCase(Target.Value) = "5th" Then

Columns("N:U").EntireColumn.Hidden = True
Columns("D:K").EntireColumn.Hidden = True
ElseIf LCase(Target.Value) = "6th" Then

Columns("P:U").EntireColumn.Hidden = True
Columns("D:M").EntireColumn.Hidden = True
ElseIf LCase(Target.Value) = "7th" Then

Columns("R:U").EntireColumn.Hidden = True
Columns("D:O").EntireColumn.Hidden = True
ElseIf LCase(Target.Value) = "8th" Then

Columns("T:U").EntireColumn.Hidden = True
Columns("D:Q").EntireColumn.Hidden = True
ElseIf LCase(Target.Value) = "9th" Then

Columns("D:S").EntireColumn.Hidden = True


Else

Columns("D:U").EntireColumn.Hidden = False

End If




End If



End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
However if I select an empty cell value and then select '2nd' then it works fine.

That's because you only unhide columns if C3 is blank (or not equal to "1st", "2nd", ... "9th").
Try:

Code:
Application.ScreenUpdating = False
Columns("D:U").EntireColumn.Hidden = False

Select Case LCase(Target.Value)
Case "1st"
    Columns("F:U").EntireColumn.Hidden = True

'etc

Case "9th"
    Columns("D:S").EntireColumn.Hidden = True
End Select

Application.ScreenUpdating = True

Second issue that I am having is that I am not able to name it anything else such as 'Game 1', 'Game 2' etc Is there some sort of restriction?

Sorry, it's not clear. What are you trying to name "Game 1"? If it's a range name, you can't have blank spaces in the name.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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