# need to add cell to this VBA code

#### williamu

##### New Member
Need to add Cells D7, F7, H7, J7, L7, N7, P7, R7 can't get it to work.

Private Sub Worksheet Calculate()

If Target.Cells.Count <> 1 Then Exit Sub
If Target.Address <> "\$B\$7" Then Exit Sub
If (Target.Value >= 0) And (Target.Value < 0) Then Exit Sub

Pictures("Picture 7").Visible = (Target.Value >= 0)
Pictures("Picture 20").Visible = (Target.Value < 0)
End Sub

### Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

#### DanteAmor

##### Well-known Member
What do you need to do, that is, what is the ultimate goal?

Maybe you need the Change event

Code:
``````Private Sub Worksheet_Change(ByVal Target As Range)

End Sub``````

#### DanteAmor

##### Well-known Member
What do you need to validate in this condition.
Concidence is not fulfilled, since no value can be greater than 0 and at the same time be less than 0.

Code:
``If (Target.Value >= 0) And (Target.Value < 0) Then Exit Sub``

#### williamu

##### New Member
when B7 is greater than 0 it displays a picture if less than 0 it displays a different picture it works fine for B7. what I trying to do is add Cells
D7, F7, H7, J7, L7, N7, P7, R7, the cells are on the same worksheet.

#### DanteAmor

##### Well-known Member
But you must put it in the change event.
In the Calculate event, Target does not exist.

But, try this:
Code:
``````  If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("B7, D7, F7, H7, J7, L7, N7, P7, R7")) Is Nothing Then
[COLOR=#ff0000]    If (Target.Value >= 0) And (Target.Value < 0) Then Exit Sub 'This line is not necessary.[/COLOR]

Pictures("Picture 7").Visible = (Target.Value >= 0)
Pictures("Picture 20").Visible = (Target.Value < 0)
End If``````

#### williamu

##### New Member
can't use change event target address is a formula

#### DanteAmor

##### Well-known Member I did not understand, is that an error message?

Try this

Code:
``````Private Sub Worksheet_Calculate()
If Target.Cells.Count <> 1 Then Exit Sub
'If Target.Address <> "\$B\$7" Then Exit Sub
Case "\$B\$7", "\$D\$7", "\$F\$7", "\$H\$7", "\$J\$7", "\$L\$7", "\$N47", "\$P\$7", "\$R\$7"
If (Target.Value >= 0) And (Target.Value < 0) Then Exit Sub

Pictures("Picture 7").Visible = (Target.Value >= 0)
Pictures("Picture 20").Visible = (Target.Value < 0)
End Select
End Sub``````
But the above does not make sense, I insist target does not exist in the calculate event.

Can you explain what you are doing, that is, you capture data in those cells and want to execute something?
In those cells you put values ​​or is it the result of a formula?

Last edited:

#### williamu

##### New Member
the target cell is formula in percentage of two numbers if its above 0 it inserts a picture (green Arrow up) ifs it below 0 it inserts a picture (red arrow down), I have 9 different columns with this formula
B7, D7, F7, H7, J7, L7, N7, P7, R7 when I
set up the VBA with column B7 it works fine, no I'm trying to accomplish this for the other columns.

#### DanteAmor

##### Well-known Member
I don't really understand how it works for you. Anyway, here I put your macro and add a cell, so that you add the others

Code:
``````Private Sub Worksheet Calculate()

If Target.Cells.Count <> 1 Then Exit Sub
If (Target.Value >= 0) And (Target.Value < 0) Then Exit Sub

Pictures("Picture 7").Visible = (Target.Value >= 0)
Pictures("Picture 20").Visible = (Target.Value < 0)
End Sub``````

Last edited: