Hiding column based on cell value

ajitexcel

Board Regular
Joined
Feb 27, 2012
Messages
66
Office Version
  1. 2019
Platform
  1. MacOS
Hi all
I am trying following code to hide/ unhide columns based on value in cell A1 but it's giving error "Function or Sub not defined"
please help

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Changed As Range

Set Changed = Range("A1")
If Not Intersect(Target, Changed) Is Nothing Then
Range("B:FF").EntireColumn.Hidden = False
Select Case UCase(Target.Value)
Case "40"
Range("AQ:FF").EntireColumn.Hidden = True
Case "120"
Range("CF:FF").EntireColumn.Hidden = True
Case "160"
Range("DQ:FF").EntireColumn.Hidden = True
End Select

End If

Set Changed = Nothing
End Sub

cell A1 is a dropdown list

am not able to find the reason.


Thanks
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Your code seems to work fine for me. It is not returning any errors.
Are you sure that it doesn't have a problem with some other VBA code that you may have?
If you hit debug, does it highlight a line of code that it thinks is the problem?

Note: If your entries are numbers, why are you applying "UCASE" to them? That has no effect on numbers, only text.
 
Upvote 0
Hi Joe
Thanks for the reply, it was highlighting line "If not intersect.....
anyways I changed the code to
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cl As Range, rTest As Range
Set rTest = Range("c1")
If rTest.Value = "" Then
Range("f:Fi").EntireColumn.Hidden = False
ElseIf rTest.Value < 41 Then
Range("F:DU").EntireColumn.Hidden = True
ElseIf rTest.Value < 121 Then
Range("f:AS").EntireColumn.Hidden = True
Range("AT:DU").EntireColumn.Hidden = False
ElseIf rTest.Value = 160 Then
Range("f:Fi").EntireColumn.Hidden = False
End If
End Sub
and it's working fine

How ever can I make this dynamic that is
if user enter's 20 then 20 columns after first 5 to hide or if he enters 50 then first 50 after 5 to hide.
or for that matter any number..

Thanks
 
Upvote 0
You can prompt the user for input using InputBox.
See: http://www.excel-easy.com/vba/examples/inputbox-function.html

You can also refer to columns numerically. So here is a little example that shows you how to do what you want:
Code:
Sub MyColumnHide()

    Dim numColsHide As Long
    
'   Request how many columns to hide
    numColsHide = InputBox("How many columns would you like to hide?")
    
'   Hide the number of columns, starting at column 6
    If numColsHide > 0 Then
        Range(Cells(1, 6), Cells(1, numColsHide + 5)).EntireColumn.Hidden = True
    End If
        
End Sub
 
Upvote 0
Hi Joe
This works fine one problem though, if I hide 20 columns first then enter 10 it does not unhide 10 columns. and I want to pick value from cell C1 (as it is dependent on other cell values).
Thanks again
 
Upvote 0
This works fine one problem though, if I hide 20 columns first then enter 10 it does not unhide 10 columns.
Just add a line of code at the very beginning of your code that unhides ALL columns initially, like this:
Code:
Columns.EntireColumn.Hidden = False
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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