Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to Evaluate formula results in VBA

Hello,

I am trying to add VBA code in Excel to evaluate the results of a Formula in
a specific cell and then to use the result in an If statement.
The macro loops thru each row, and I want to compare the formula result in a
specific cell to see if the result is equal to 0. If the result is equal to 0
then I want to change the value of the cell to a text string.

Here is what I have so far.

Dim intRows As Integer
intRows = Range("C1").CurrentRegion.Rows.Count
'Loop through all rows
For i = 2 To intRows
If Application.Evaluate("AJ & i") = 0 Then
Range("AJ" & i).Value = "USED"
End If

Thanks in advance for your help.

Jeff
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default How to Evaluate formula results in VBA

Why not just simply

If Range("AJ" & i) = 0 then
Range("AJ" & i) = "USED"
End If

I didn't specify .Value because .Value is the default property anyhow. Oh -
and don't forget the Next to close the For loop.

"Fourtrax" wrote:

Hello,

I am trying to add VBA code in Excel to evaluate the results of a Formula in
a specific cell and then to use the result in an If statement.
The macro loops thru each row, and I want to compare the formula result in a
specific cell to see if the result is equal to 0. If the result is equal to 0
then I want to change the value of the cell to a text string.

Here is what I have so far.

Dim intRows As Integer
intRows = Range("C1").CurrentRegion.Rows.Count
'Loop through all rows
For i = 2 To intRows
If Application.Evaluate("AJ & i") = 0 Then
Range("AJ" & i).Value = "USED"
End If

Thanks in advance for your help.

Jeff

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to Evaluate formula results in VBA

I tried what you suggested and I get the following error:
Run time error 13 - type mismatch

It errors on the line:
If Range("AJ" & i) = 0 Then

Any ideas?

"JLatham" wrote:

Why not just simply

If Range("AJ" & i) = 0 then
Range("AJ" & i) = "USED"
End If

I didn't specify .Value because .Value is the default property anyhow. Oh -
and don't forget the Next to close the For loop.

"Fourtrax" wrote:

Hello,

I am trying to add VBA code in Excel to evaluate the results of a Formula in
a specific cell and then to use the result in an If statement.
The macro loops thru each row, and I want to compare the formula result in a
specific cell to see if the result is equal to 0. If the result is equal to 0
then I want to change the value of the cell to a text string.

Here is what I have so far.

Dim intRows As Integer
intRows = Range("C1").CurrentRegion.Rows.Count
'Loop through all rows
For i = 2 To intRows
If Application.Evaluate("AJ & i") = 0 Then
Range("AJ" & i).Value = "USED"
End If

Thanks in advance for your help.

Jeff

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default How to Evaluate formula results in VBA

Try changing the type for intRows to Long vs Integer, I don't know how large
your range is, but with potentially over 65,000 rows, an Integer won't hack
the mission. To check if that's what happening, when it errors out, go into
debug and find out value of i at that point, either by hovering over it or
going into the Immediate window and using Print i to see what it is.

I just ran this code against a few rows, some with zero in them, some with
words "NOT USED" (so I could make sure they weren't getting overwritten) and
even some completely empty cells, and all with zeros or that were empty ended
up with "USED" in them. Matter of fact I even changed the For I = 1 to 11 to
'For I = 1 to Rows.Count' and it ran fine - now I have a column of 65536
"USED" entries ;-)

Sub Testing()
Dim I As Long
For I = 1 To 11
If Range("AJ" & I) = 0 Then
Range("AJ" & I) = "USED"
End If
Next
End Sub


"Fourtrax" wrote:

I tried what you suggested and I get the following error:
Run time error 13 - type mismatch

It errors on the line:
If Range("AJ" & i) = 0 Then

Any ideas?

"JLatham" wrote:

Why not just simply

If Range("AJ" & i) = 0 then
Range("AJ" & i) = "USED"
End If

I didn't specify .Value because .Value is the default property anyhow. Oh -
and don't forget the Next to close the For loop.

"Fourtrax" wrote:

Hello,

I am trying to add VBA code in Excel to evaluate the results of a Formula in
a specific cell and then to use the result in an If statement.
The macro loops thru each row, and I want to compare the formula result in a
specific cell to see if the result is equal to 0. If the result is equal to 0
then I want to change the value of the cell to a text string.

Here is what I have so far.

Dim intRows As Integer
intRows = Range("C1").CurrentRegion.Rows.Count
'Loop through all rows
For i = 2 To intRows
If Application.Evaluate("AJ & i") = 0 Then
Range("AJ" & i).Value = "USED"
End If

Thanks in advance for your help.

Jeff

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default How to Evaluate formula results in VBA

Oh, if value of 'i' seems acceptable, go take a look on the worksheet itself
at the cell that is being pointed to and see what is in it. Might be
something special or unexpected.

"Fourtrax" wrote:

I tried what you suggested and I get the following error:
Run time error 13 - type mismatch

It errors on the line:
If Range("AJ" & i) = 0 Then

Any ideas?

"JLatham" wrote:

Why not just simply

If Range("AJ" & i) = 0 then
Range("AJ" & i) = "USED"
End If

I didn't specify .Value because .Value is the default property anyhow. Oh -
and don't forget the Next to close the For loop.

"Fourtrax" wrote:

Hello,

I am trying to add VBA code in Excel to evaluate the results of a Formula in
a specific cell and then to use the result in an If statement.
The macro loops thru each row, and I want to compare the formula result in a
specific cell to see if the result is equal to 0. If the result is equal to 0
then I want to change the value of the cell to a text string.

Here is what I have so far.

Dim intRows As Integer
intRows = Range("C1").CurrentRegion.Rows.Count
'Loop through all rows
For i = 2 To intRows
If Application.Evaluate("AJ & i") = 0 Then
Range("AJ" & i).Value = "USED"
End If

Thanks in advance for your help.

Jeff



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to Evaluate formula results in VBA

Got it! It was the value in the cell itself. I did not have ISERROR in the
function so it was trying to evaluate an error code. Once I fixed the error
code it works. Thanks for your help.

"JLatham" wrote:

Oh, if value of 'i' seems acceptable, go take a look on the worksheet itself
at the cell that is being pointed to and see what is in it. Might be
something special or unexpected.

"Fourtrax" wrote:

I tried what you suggested and I get the following error:
Run time error 13 - type mismatch

It errors on the line:
If Range("AJ" & i) = 0 Then

Any ideas?

"JLatham" wrote:

Why not just simply

If Range("AJ" & i) = 0 then
Range("AJ" & i) = "USED"
End If

I didn't specify .Value because .Value is the default property anyhow. Oh -
and don't forget the Next to close the For loop.

"Fourtrax" wrote:

Hello,

I am trying to add VBA code in Excel to evaluate the results of a Formula in
a specific cell and then to use the result in an If statement.
The macro loops thru each row, and I want to compare the formula result in a
specific cell to see if the result is equal to 0. If the result is equal to 0
then I want to change the value of the cell to a text string.

Here is what I have so far.

Dim intRows As Integer
intRows = Range("C1").CurrentRegion.Rows.Count
'Loop through all rows
For i = 2 To intRows
If Application.Evaluate("AJ & i") = 0 Then
Range("AJ" & i).Value = "USED"
End If

Thanks in advance for your help.

Jeff

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula to evaluate dates bigbird98 Excel Worksheet Functions 2 July 2nd 08 09:43 PM
Might be a bug in Evaluate Formula? cyx Excel Discussion (Misc queries) 2 May 2nd 07 10:52 PM
What is evaluate formula? Rasoul Khoshravan Excel Worksheet Functions 11 October 27th 06 01:52 PM
Formula Will Not Evaluate Cecil Excel Worksheet Functions 3 April 25th 06 07:38 PM
Evaluate formula using VBA Ali Baba Excel Discussion (Misc queries) 0 August 17th 05 12:31 AM


All times are GMT +1. The time now is 08:40 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"