Writing an IF/Then Statement in VB based on entry in a cell

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have defined the following value for a cell within my macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other things. I'm not sure
how to write an "IF" statement to capture this information. How do I do
that?

Thanks in advance,
Barb Reinhardt
 
Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If
 
Range("K" & i).Activate
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"
ActiveCell.Value > 0 Then
'do something
Else
'do something else
End If


Hutch
 
Thanks. The ActiveCell.Value part was what I needed.

Now I have another question. How do I check to see If ActiveCell.Value =
#N/A. I can't seem to figure this one out.


bigwheel said:
Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If

Barb Reinhardt said:
I have defined the following value for a cell within my macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other things. I'm not sure
how to write an "IF" statement to capture this information. How do I do
that?

Thanks in advance,
Barb Reinhardt
 
Barb,
Now I have another question. How do I check to see If
ActiveCell.Value =
#N/A. I can't seem to figure this one out.

Try

If Application.WorksheetFunction.IsNA(ActiveCell.Value) Then
Debug.Print "is n/a"
Else
Debug.Print "not n/a"
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



in message
Thanks. The ActiveCell.Value part was what I needed.

Now I have another question. How do I check to see If
ActiveCell.Value =
#N/A. I can't seem to figure this one out.


bigwheel said:
Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If

Barb Reinhardt said:
I have defined the following value for a cell within my
macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _

"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other
things. I'm not sure
how to write an "IF" statement to capture this information.
How do I do
that?

Thanks in advance,
Barb Reinhardt
 
One more way:

if activecell.text = "#N/A" then



Barb said:
Thanks. The ActiveCell.Value part was what I needed.

Now I have another question. How do I check to see If ActiveCell.Value =
#N/A. I can't seem to figure this one out.

bigwheel said:
Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If

Barb Reinhardt said:
I have defined the following value for a cell within my macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other things. I'm not sure
how to write an "IF" statement to capture this information. How do I do
that?

Thanks in advance,
Barb Reinhardt
 
How about if the cell value is #Value! ???

Dave Peterson said:
One more way:

if activecell.text = "#N/A" then



Barb said:
Thanks. The ActiveCell.Value part was what I needed.

Now I have another question. How do I check to see If ActiveCell.Value =
#N/A. I can't seem to figure this one out.

bigwheel said:
Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If

:

I have defined the following value for a cell within my macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other things. I'm not sure
how to write an "IF" statement to capture this information. How do I do
that?

Thanks in advance,
Barb Reinhardt
 
if activecell.text = "#Value!" then

or

if iserror(activecell.value) then
to catch all kinds of errors



Barb said:
How about if the cell value is #Value! ???

Dave Peterson said:
One more way:

if activecell.text = "#N/A" then



Barb said:
Thanks. The ActiveCell.Value part was what I needed.

Now I have another question. How do I check to see If ActiveCell.Value =
#N/A. I can't seem to figure this one out.

:

Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If

:

I have defined the following value for a cell within my macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other things. I'm not sure
how to write an "IF" statement to capture this information. How do I do
that?

Thanks in advance,
Barb Reinhardt
 
For some reason, it didn't work as I expected ... and then I realized I'd
goofed something else up. Thanks.

Dave Peterson said:
if activecell.text = "#Value!" then

or

if iserror(activecell.value) then
to catch all kinds of errors



Barb said:
How about if the cell value is #Value! ???

Dave Peterson said:
One more way:

if activecell.text = "#N/A" then



Barb Reinhardt wrote:

Thanks. The ActiveCell.Value part was what I needed.

Now I have another question. How do I check to see If ActiveCell.Value =
#N/A. I can't seem to figure this one out.

:

Something like this perhaps:-

If ActiveCell.Value > 0 Then
' put here what you want to happen i.e.
MsgBox "it's working!!"
End If

:

I have defined the following value for a cell within my macro

Range("K" & i).Select
ActiveCell.FormulaR1C1 = _
"=((YEAR(RC[-1])-YEAR(RC[-2]))*12)+(MONTH(RC[-1])-MONTH(RC[-2]))"

If the value of this range is > 0, I want to do other things. I'm not sure
how to write an "IF" statement to capture this information. How do I do
that?

Thanks in advance,
Barb Reinhardt
 

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

Back
Top