Streamline this VBA

Atholl

Active Member
Joined
May 19, 2002
Messages
436
Hi all,

The following code works a kind of traffic light system which colours a shape and brings it to the front depending on the contents of a cell (K18). The code works perfectly for my 28 shapes but I feel I could streamline this as it's veeerrrrrry long winded! Unfortunately, everything I've tried thus far hasn't worked.

Has anyone any suggestions?

Atholl

Code:
If Worksheets("Summary").Range("K18") = "A" Then
     ActiveSheet.Shapes("SHAPE 1").ZOrder msoBringToFront
     ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 12
     ActiveSheet.Shapes("SHAPE 1").Fill.Solid
     ActiveSheet.Shapes("SHAPE 1").Fill.Transparency = 0.5
    
    ElseIf Worksheets("Summary").Range("K18") = "B" Then
    ActiveSheet.Shapes("SHAPE 1").ZOrder msoBringToFront
    ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 57
    ActiveSheet.Shapes("SHAPE 1").Fill.Solid
    ActiveSheet.Shapes("SHAPE 1").Fill.Transparency = 0.5
    
    ElseIf Worksheets("Summary").Range("K18") = "C" Then
    ActiveSheet.Shapes("SHAPE 1").ZOrder msoBringToFront
    ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 52
    ActiveSheet.Shapes("SHAPE 1").Fill.Solid
    ActiveSheet.Shapes("SHAPE 1").Fill.Transparency = 0.5
    
    ElseIf Worksheets("Summary").Range("K18") = "D" Then
    ActiveSheet.Shapes("SHAPE 1").ZOrder msoBringToFront
    ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 10
    ActiveSheet.Shapes("SHAPE 1").Fill.Solid
    ActiveSheet.Shapes("SHAPE 1").Fill.Transparency = 0.5
    
    ElseIf Worksheets("Summary").Range("K18") = "0" Then
    ActiveSheet.Shapes("SHAPE 1").ZOrder msoSendToBack

End If
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hello,

How about

Code:
Sub tred()
Select Case Sheets("Summary").Range("K18").Value
    Case "A"
        BASICS
        ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 12
    Case "B"
        BASICS
    ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 57
    Case "C"
        BASICS
    ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 52
    Case "D"
        BASICS
    ActiveSheet.Shapes("SHAPE 1").Fill.ForeColor.SchemeColor = 10
    Case "O"
    ActiveSheet.Shapes("SHAPE 1").ZOrder msoSendToBack
End Select
End Sub
Sub BASICS()
     ActiveSheet.Shapes("SHAPE 1").ZOrder msoBringToFront
     ActiveSheet.Shapes("SHAPE 1").Fill.Solid
     ActiveSheet.Shapes("SHAPE 1").Fill.Transparency = 0.5
End Sub
 
Upvote 0
here is part of your code, untested but should work.

Sub col_shape()

With ActiveSheet.Shapes("SHAPE1")
.ZOrder msoBringToFront
.Fill.ForeColor.SchemeColor = 12
.Fill.Solid
.Fill.Transparency = 0.5
End With

End Sub
 
Upvote 0
Hey guys,

Thanks for the input!

Is there a way to 'genericise' the 'BASICS' sub so that it doesn't have to be repeated another 27 times?

Atholl
 
Upvote 0
Hi Atholl

You could pass the name of the shape (or possibly a reference to the actual object) to the Sub as an argument:

Code:
Sub Basics(ShName As String)
....
With ActiveSheet.SHAPES(ShName)
....

Best regards

Richard
 
Upvote 0
Hi Richard,

I was attempting to implement your suggestion to no avail; can you elaborate for a VB dunce?!

Atholl
 
Upvote 0
Hi Atholl

This works for me:

Code:
Sub test()
shaName = ActiveSheet.Shapes(1).Name
changer (shaName)
End Sub
Sub changer(shaName As String)
With ActiveSheet.Shapes(shaName)
    .Fill.ForeColor.SchemeColor = 12
    .Fill.Solid
End With
End Sub

or as an Object ref:

Code:
Sub test()
Dim shaName As Shape
 Set shaName = ActiveSheet.Shapes(1)
changer shaName
End Sub
Sub changer(shap As Object)
With shap
    .Fill.ForeColor.SchemeColor = 6
    .Fill.Solid
End With
End Sub

Best regards

Richard
 
Upvote 0

Forum statistics

Threads
1,216,380
Messages
6,130,291
Members
449,570
Latest member
TomMacca52

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