Multiple private subs in worksheet

Lord_B

New Member
Joined
Oct 17, 2018
Messages
19
Hi,

This is my first post, hope get this right.

I am newish to VBA and have come up against problem (first of many no doubt). I have searched for an answer on the forums and Google but not finding one that works for me.
I need to put multiple private subs in one work sheet but am unable to run the code, the first part works but not the other bits.

I hope the below makes sense?


Code:
Private Sub Worksheet_Calculate()
     If Range("a5").Value = "a" Then
          Me.Shapes("Rectangle: Rounded Corners 1").Visible = True
     Else
            Me.Shapes("Rectangle: Rounded Corners 1").Visible = False
     End If
End Sub
 
Private Sub Worksheet_Calculate()
     If Range("a6").Value = "a" Then
          Me.Shapes("Rectangle: Rounded Corners 4").Visible = True
     Else
            Me.Shapes("Rectangle: Rounded Corners 4").Visible = False
     End If
End Sub
 
 
Private Sub Worksheet_Calculate()
     If Range("a7").Value = "a" Then
          Me.Shapes("Rectangle: Rounded Corners 5").Visible = True
     Else
            Me.Shapes("Rectangle: Rounded Corners 5").Visible = False
     End If
End Sub

Do I need to combine these somehow?

Hopefully this a rookie mistake I have made.

Thanks for looking/helping on this,

Ben
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the Board!

You are not allowed to have multiple Procedures in the same module with the same name. You need to combine them all into one single procedure.
In this case, that is pretty easy to do, as it doesn't appear that you have any interference between them, i.e.
Code:
Private Sub Worksheet_Calculate()

     If Range("a5").Value = "a" Then
          Me.Shapes("Rectangle: Rounded Corners 1").Visible = True
     Else
            Me.Shapes("Rectangle: Rounded Corners 1").Visible = False
     End If

     If Range("a6").Value = "a" Then
          Me.Shapes("Rectangle: Rounded Corners 4").Visible = True
     Else
            Me.Shapes("Rectangle: Rounded Corners 4").Visible = False
     End If

     If Range("a7").Value = "a" Then
          Me.Shapes("Rectangle: Rounded Corners 5").Visible = True
     Else
            Me.Shapes("Rectangle: Rounded Corners 5").Visible = False
     End If

End Sub
 
Last edited:
Upvote 0
Hi & welcome to MrExcel.

You can only have one Calculate event per sheet, so you need to combine them like
Code:
Private Sub Worksheet_Calculate()
   If Range("a5").Value = "a" Then
      Me.Shapes("Rectangle: Rounded Corners 1").Visible = True
   Else
      Me.Shapes("Rectangle: Rounded Corners 1").Visible = False
   End If
   If Range("a6").Value = "a" Then
      Me.Shapes("Rectangle: Rounded Corners 4").Visible = True
   Else
      Me.Shapes("Rectangle: Rounded Corners 4").Visible = False
   End If
   If Range("a7").Value = "a" Then
      Me.Shapes("Rectangle: Rounded Corners 5").Visible = True
   Else
      Me.Shapes("Rectangle: Rounded Corners 5").Visible = False
   End If
End Sub
 
Upvote 0
Thanks Joe4 & Fluff for the warm welcome,

Wow! when you put the code like that it makes sense!

Thanks to both of you for your help. you make it look soooo simple :)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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