Converting VBA code from a Button to a Change Event

Hainesy

New Member
Joined
Jan 10, 2018
Messages
12
Hi all,

I have some code attributed to a button currently and I want to change it so that it runs automatically based on the value of a cell.

I believe it needs to be a change event? But I cant seem to get it to work.

Below is the code that I currently have running off a button.

Code:
Sub QuestionOne()


Application.ScreenUpdating = False


ActiveSheet.Unprotect "Password"


If [m34] = "Yes" Then
    Rows("35:38").Hidden = False


ElseIf [m34] = "No" Then
    Rows("35:38").Hidden = True
    
    Range("M35:M38").Select
    Selection.ClearContents
    
    Range("M34").Select
    
End If


ActiveSheet.Protect "Password"


Application.ScreenUpdating = True


End Sub

Any help/advice would be much appreciated.

Thank you
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Thanks!

M34 is being changed manually by the user. It contains a 'Yes', 'No' validation list.
 
Upvote 0
Hi, in that case you can try this. Right click the sheet in question and choose "View Code" and paste the code in the sheets code module.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("M34"), Target) Is Nothing Then
  Me.Unprotect "Password"
  Select Case UCase(Range("M34").Value)
    Case "YES"
      Rows("35:38").Hidden = False
    Case "NO"
      Rows("35:38").Hidden = True
      Application.EnableEvents = False
      Range("M35:M38").ClearContents
      Application.EnableEvents = True
  End Select
  Me.Protect "Password"
End If
End Sub
 
Upvote 0
Hi, in that case you can try this. Right click the sheet in question and choose "View Code" and paste the code in the sheets code module.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("M34"), Target) Is Nothing Then
  Me.Unprotect "Password"
  Select Case UCase(Range("M34").Value)
    Case "YES"
      Rows("35:38").Hidden = False
    Case "NO"
      Rows("35:38").Hidden = True
      Application.EnableEvents = False
      Range("M35:M38").ClearContents
      Application.EnableEvents = True
  End Select
  Me.Protect "Password"
End If
End Sub

Thank you for coming back to me.

If I use that code in a blank workbook, it seems to work fine. But for some reason it doesn't seem to work in my actual workbook.

Could there be some reason for this?
 
Upvote 0
Apologies, it was my fault! Its now working fine (I had pasted it into the wrong sheet!)

That's exactly what I needed, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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