Streamline this VBA

Atholl

Active Member
Joined
May 19, 2002
Messages
434
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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
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
 

jwchu3

Board Regular
Joined
Jan 22, 2006
Messages
85
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
 

Atholl

Active Member
Joined
May 19, 2002
Messages
434

ADVERTISEMENT

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
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

Atholl

Active Member
Joined
May 19, 2002
Messages
434

ADVERTISEMENT

Hi Richard,

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

Atholl
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
 

Forum statistics

Threads
1,136,353
Messages
5,675,288
Members
419,559
Latest member
BraytonM

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
Top