Looping in VBA

Nush1981

Board Regular
Joined
Oct 24, 2006
Messages
92
Can someone please provide me with the script to loop the following VBA:-

If Range("J10").Value = 0 Then
ActiveSheet.Shapes("Oval 428").Fill.ForeColor.SchemeColor = 1

Else

If Range("W10").Value > 0 Then
ActiveSheet.Shapes("Oval 428").Fill.ForeColor.SchemeColor = 10

Else

If Range("W10").Value < 0 Then
ActiveSheet.Shapes("Oval 428").Fill.ForeColor.SchemeColor = 50

Else

If Range("W10").Value = 0 Then
ActiveSheet.Shapes("Oval 428").Fill.ForeColor.SchemeColor = 43

End If
End If
End If
End If

If Range("J13").Value = 0 Then
ActiveSheet.Shapes("Oval 303").Fill.ForeColor.SchemeColor = 1

Else

If Range("W13").Value > 0 Then
ActiveSheet.Shapes("Oval 303").Fill.ForeColor.SchemeColor = 10

Else

If Range("W13").Value < 0 Then
ActiveSheet.Shapes("Oval 303").Fill.ForeColor.SchemeColor = 50

Else

If Range("W13").Value = 0 Then
ActiveSheet.Shapes("Oval 303").Fill.ForeColor.SchemeColor = 43

End If
End If
End If
End If


If Range("J16").Value = 0 Then
ActiveSheet.Shapes("Oval 413").Fill.ForeColor.SchemeColor = 1

Else

If Range("W16").Value > 0 Then
ActiveSheet.Shapes("Oval 413").Fill.ForeColor.SchemeColor = 10

Else

If Range("W16").Value < 0 Then
ActiveSheet.Shapes("Oval 413").Fill.ForeColor.SchemeColor = 50

Else

If Range("W16").Value = 0 Then
ActiveSheet.Shapes("Oval 413").Fill.ForeColor.SchemeColor = 43

End If
End If
End If
End If

If Range("J19").Value = 0 Then
ActiveSheet.Shapes("Oval 304").Fill.ForeColor.SchemeColor = 1

Else

If Range("W19").Value > 0 Then
ActiveSheet.Shapes("Oval 304").Fill.ForeColor.SchemeColor = 10

Else

If Range("W19").Value < 0 Then
ActiveSheet.Shapes("Oval 304").Fill.ForeColor.SchemeColor = 50

Else

If Range("W19").Value = 0 Then
ActiveSheet.Shapes("Oval 304").Fill.ForeColor.SchemeColor = 43

End If
End If
End If
End If

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
There may be more efficient methods, but the following SHOULD work (please note - untested!)

Code:
Sub test()
    Dim n As Long
    Dim Oval As Long

    Application.ScreenUpdating = False

    n = 10
    Do While n <= 19

        Select Case n
        Case 10: Oval = 428
        Case 13: Oval = 303
        Case 16: Oval = 413
        Case 19: Oval = 304
        End Select

        If Range("J10").Value = 0 Then ActiveSheet.Shapes("Oval " & Oval).Fill.ForeColor.SchemeColor = 1

        With Range("W" & n)

            If .Value > 0 Then ActiveSheet.Shapes("Oval " & Oval).Fill.ForeColor.SchemeColor = 10
            If .Value < 0 Then ActiveSheet.Shapes("Oval " & Oval).Fill.ForeColor.SchemeColor = 50
            If .Value = 0 Then ActiveSheet.Shapes("Oval " & Oval).Fill.ForeColor.SchemeColor = 43

        End With
        n = n + 3
    Loop

    Application.ScreenUpdating = True

End Sub

EDIT: Had to change a ">" to "<"
 

Forum statistics

Threads
1,181,102
Messages
5,928,063
Members
436,586
Latest member
latintxn

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