VBA code, value in a range

A

Andre

Hello.

I'm trying to write some VBA code and i have to calculate a difference
between two cells.

Here it is:

A1 = 30
A2 = 31

if A1+-1 = A2 then "OK"

How to put it in code this +-1 ??


TIA
 
A

Andre

isabelle napisał(a):
If Abs(A2 - A1) <= 1 Then MsgBox "OK"

Thanks a lot. That works :)

Now. Can you help me with absolute cell addresses in such code:

Sheets("sheet2").Select
Range("A1").Select
val = "=sheet2!R[38]C[-7]-sheet2!R[50]C[-7]"
Sheets("sheet1").Select
Range("J3").Select
Selection.Value = val
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??
_);_(@_)"
Application.CutCopyMode = False


I have no idea how this address format works (refering to "val"
calculations) so is there any possibility to change addresses to any
understadable format? :)
 
D

Don Guillett

isabelle napisał(a):
If Abs(A2 - A1) <= 1 Then MsgBox "OK"

Thanks a lot. That works :)

Now. Can you help me with absolute cell addresses in such code:

Sheets("sheet2").Select
    Range("A1").Select
    val = "=sheet2!R[38]C[-7]-sheet2!R[50]C[-7]"
Sheets("sheet1").Select
    Range("J3").Select
    Selection.Value = val
    Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??
_);_(@_)"
    Application.CutCopyMode = False

I have no idea how this address format works (refering to "val"
calculations) so is there any possibility to change addresses to any
understadable format? :)

example for all in ONE line broken by continuation character (space
and unerscore) for readability
Selections are almost never necessary or desirable

sheets("sheet1").range("j3").value= _
sheets("sheet2").range("c2")-sheets("sheet2").range("c3")
or

set ds = sheets("sheet1")
set ss=sheets("sheet2")
ds.range("j3").value=ss.range("c2")-ss.range("c3")
 

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