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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
: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:
Upvote 0
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.
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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