VBA - SelectionChange not working

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
594
Office Version
  1. 365
Hi

I have the following code on Sheet3 of my workbook. Unfortunately, when C16 is either left blank, or "standard" is inserted, nothing happens - i.e. it does not hide rows or columns.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.EnableEvents = True
    
    
       ActiveSheet.Unprotect
    If Range("C16").Value = "" Then
        Columns("O:P").EntireColumn.Hidden = True
     Rows("18").EntireRow.Hidden = False
        Rows("19:20").EntireRow.Hidden = True
        
          ActiveSheet.Unprotect
    If Range("C16").Value = "Standard" Then
        Columns("O:P").EntireColumn.Hidden = True
     Rows("18").EntireRow.Hidden = False
        Rows("19:20").EntireRow.Hidden = True

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Have you tried changing the range to .Text instead of .Value?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    Application.EnableEvents = True
    
    
       ActiveSheet.Unprotect
    If Range("C16").Text = "" Then
        Columns("O:P").EntireColumn.Hidden = True
     Rows("18").EntireRow.Hidden = False
        Rows("19:20").EntireRow.Hidden = True
        
          ActiveSheet.Unprotect
    If Range("C16").Text = "Standard" Then
        Columns("O:P").EntireColumn.Hidden = True
     Rows("18").EntireRow.Hidden = False
        Rows("19:20").EntireRow.Hidden = True


End Sub
 
Upvote 0
Unfortunately, no that hasn't helped. Also realise the above does not include End If statements, I have added these, and still doesn't work :9

Have you tried changing the range to .Text instead of .Value?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    Application.EnableEvents = True
    
    
       ActiveSheet.Unprotect
    If Range("C16").Text = "" Then
        Columns("O:P").EntireColumn.Hidden = True
     Rows("18").EntireRow.Hidden = False
        Rows("19:20").EntireRow.Hidden = True
        
          ActiveSheet.Unprotect
    If Range("C16").Text = "Standard" Then
        Columns("O:P").EntireColumn.Hidden = True
     Rows("18").EntireRow.Hidden = False
        Rows("19:20").EntireRow.Hidden = True


End Sub
 
Upvote 0
Try running this & then try changing cells
Code:
Sub Chk()
Application.EnableEvents = True
End Sub
 
Upvote 0
Where does this go please? On my Sheet3 or ThisWorkbook?

Also, does my cde need to go onto Sheet3 or ThisWorkbook?

Try running this & then try changing cells
Code:
Sub Chk()
Application.EnableEvents = True
End Sub
 
Upvote 0
Your code needs to go in the sheet module for the sheet you want it to work on.
My code can go anywhere
 
Upvote 0
Hi

I have tried this, and run your macro, then saved workbook, but (my) above code, on sheet I want to hide rows/columns on still does not work. I have tried code in a blank workbook and it works - so unsure as to why?

Your code needs to go in the sheet module for the sheet you want it to work on.
My code can go anywhere
 
Last edited:
Upvote 0
Ignore please - just realised when I put this into a new workbook, it is all on different sheet numbers, so I was putting it in sheet3 which used to be my main screen, and is now sheet 1 so works perfectly. Apologies!!

Hi

I have tried this, and run your macro, then saved workbook, but (my) above code, on sheet I want to hide rows/columns on still does not work. I have tried code in a blank workbook and it works - so unsure as to why?
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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