If a cell equals then macro

G

Guest

I have a table with letters "C" or "D" in column G starting G2 & numbers in
column H. I would like the number in a cell to be negative if cell next to it
in column G = "c"
This would apply to all numbers in column starting from H2
The macro below has to be corrected
Sub neg()
If ActiveCell.Offset(0, -1) = "c" Then
ActiveCell.Value = -ActiveCell.Value
End If
End Sub

Pls help
 
N

Niek Otten

Look here:

http://www.cpearson.com/excel/Events.aspx

--
Kind regards,

Niek Otten
Microsoft MVP - Excel

|I have a table with letters "C" or "D" in column G starting G2 & numbers in
| column H. I would like the number in a cell to be negative if cell next to it
| in column G = "c"
| This would apply to all numbers in column starting from H2
| The macro below has to be corrected
| Sub neg()
| If ActiveCell.Offset(0, -1) = "c" Then
| ActiveCell.Value = -ActiveCell.Value
| End If
| End Sub
|
| Pls help
 
G

Guest

hi
try this...
Sub neg()
Dim r As Range
Dim rd As Range
Set r = Range("H2")
Do While Not IsEmpty(r)
Set rd = r.Offset(1, 0)
If r.Offset(0, -1) = "c" Then
r.Value = -r.Value
End If
Set r = rd
Loop
End Sub
regards
FSt1
 
G

Guest

Can someone pls correct my code or give me the correct macro as I'm new to
excel
thxs
 
G

Guest

Perfect - thxs a lot

Do you have another macro which can move the number to the next right hand
column (i.e .Insert Shift:=xlToRight) if cell in G = "c"??
Pls help - thxs
 
M

Mike Fogleman

Sub neg()
Dim r As Range
Dim rd As Range
Set r = Range("H2")
Do While Not IsEmpty(r)
Set rd = r.Offset(1, 0)
If r.Offset(0, -1) = "c" Then
r.Offset(0, 1) = -r.Value
r.ClearContents
End If
Set r = rd
Loop
End Sub


Mike F
 
G

Guest

hi again,
add this....
r.Insert Shift:=xlToRight
after....
r.Value = -r.Value

regards
FSt1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top