need to add cell to this VBA code

williamu

New Member
Joined
Mar 19, 2019
Messages
16
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
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,161
Office Version
2007
Platform
Windows
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
Joined
Dec 3, 2018
Messages
11,161
Office Version
2007
Platform
Windows
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
Joined
Mar 19, 2019
Messages
16
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
Joined
Dec 3, 2018
Messages
11,161
Office Version
2007
Platform
Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,161
Office Version
2007
Platform
Windows
:confused:

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
Select Case Target.Address
  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.
Is your event correct?


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
Joined
Mar 19, 2019
Messages
16
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
Joined
Dec 3, 2018
Messages
11,161
Office Version
2007
Platform
Windows
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.Address <> "$B$7" And target.address <> "$D$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
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,095,621
Messages
5,445,548
Members
405,337
Latest member
kann82

This Week's Hot Topics

Top