Selection_Change question

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,055
Office Version
  1. 365
Platform
  1. Windows
I'm using this;

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("C3").Value = 28 Then
        Columns("AK:AM").EntireColumn.Hidden = True
    Else
   If Range("C3").Value = 29 Then
        Columns("AL:AM").EntireColumn.Hidden = True
    Else
   If Range("C3").Value = 30 Then
        Columns("AM").EntireColumn.Hidden = True
   Else
        Columns("AK:AM").EntireColumn.Hidden = False
    End If
     End If
      End If
End Sub

Feel free to improve it lol..............

Anyway, my question is this - cell C3 contains a number (28-31) and the code hides columns based on this value.

However C3 is a formula driven by a data validation cell in cell C2 - so if the user changes the value in C2, the columns will not hide until you click on C3

Is there a simple fix for this?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
If I understand correctly you should use the Change Event and run your code when C2 changes value.
 
Upvote 0
Sorry..........as a VB numptie, how do I do that?
 
Upvote 0
Instead of a Selection Change event use a (Value) Change event.

Replace your code with:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Target.Address <> "$C$2" Then Exit Sub
    
    Select Case Range("C3").Value
        Case 28
            Columns("AK:AM").Hidden = True
    
        Case 29
            Columns("AM").Hidden = True
    
        Case 30
            Columns("AK:AM").Hidden = False
    End Select
End Sub
 
Upvote 0
Code:
On Error Resume Next
Set dependentCells = Target.Dependents
On Error Goto 0
If Not dependentCells is Nothing Then
    If dependentCells.Address = "$C$3" Then
        'Hide stuff
    End If
End If
 
Upvote 0
Instead of a Selection Change event use a (Value) Change event.

Replace your code with:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Target.Address <> "$C$2" Then Exit Sub
    
    Select Case Range("C3").Value
        Case 28
            Columns("AK:AM").Hidden = True
    
        Case 29
            Columns("AM").Hidden = True
    
        Case 30
            Columns("AK:AM").Hidden = False
    End Select
End Sub

I get "Compile Error - Ambiguous name detected: Worksheet_Change"

Edit: I have another Worksheet_Change in the same workbook - is that the problem?
 
Last edited:
Upvote 0
Try this,
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$C$3" Then Exit Sub
Select Case Target
        Case 28 : Columns("AK:AM").EntireColumn.Hidden = True
        Case 29 : Columns("AL:AM").EntireColumn.Hidden = True
        Case 30 : Columns("AM").EntireColumn.Hidden = True
        Case Else : Columns("AK:AM").EntireColumn.Hidden = False     
End Select
End Sub

lenze
 
Upvote 0
I get "Compile Error - Ambiguous name detected: Worksheet_Change"

Edit: I have another Worksheet_Change in the same workbook - is that the problem?

You can only have one Worksheet Change event handler per worksheet so you will need to combine the code in the two procedures together.
 
Upvote 0
What is your other Change Event? Like Colin says, you can combine them ito one.
lenze
 
Upvote 0
Edit: I have another Worksheet_Change in the same workbook - is that the problem?

Yes, there can be only one. You have to merge the 2, like

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    ' In case cell C2 changed value, the code I posted
    If Target.Address = "$C$2" Then
    
        Select Case Range("C3").Value
            Case 28
                Columns("AK:AM").Hidden = True
        
            Case 29
                Columns("AM").Hidden = True
        
            Case 30
                Columns("AK:AM").Hidden = False
        End Select
    Else
    
        ' for other changes, your other code
    
    End If
End Sub


Lenze: The code should run when C2 changes, but you will hide the columns depending on the values of C3.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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