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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,064
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,064
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,064
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,064
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,064
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,230
Messages
5,443,239
Members
405,219
Latest member
CraneS

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top