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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
HI, welcome to the forum!

Is cell M34 being changing manually or does it contain a formula?
 
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,223,842
Messages
6,174,978
Members
452,596
Latest member
Anabaric

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