Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default problem with an iif() function in a formula

I'm trying to use VBA to put a formula into a range of cells, but something
is not working right.

Here's what I'm trying to accomplish:

I have a list of numbers in column D, beginning in row 2 and ending in row
numRecords, where numRecords is module level variable that contains the row
number of the bottom number in that list in column D. I'm trying to insert
text values to the right of each of those numbers in column E such that:

1. If the number to the immediate left in column D is greater than 0.1,
insert the text string "REG".
2. If the number to the immediate left in column D is less than or equal to
0.1, leave the cell blank.

I've tried using the following code to do this:

Sub enterTP1 numRecords = Range("A1").CurrentRegion.Rows.Count
ActiveSheet.Range(Cells(2, 5), Cells(numRecords, 5)) =
IIf(Range("A1").Cells(2, 4) 0.1, "REG", "")
End Sub

This code almost works, because it inserts the text string "REG" in the
correct range of cells, so there are no problems with my range or cell
specifications or the value or scope of the variable numRows. The problem
is with the evaluation of the iif() function, because my code inserts the
"REG" string in ALL the cells in the range, even where the corresponding
values in column D are less than 0.1. So in effect, the iif() function is
not being evaluated in my code.

Can anyone tell me how to modify my code so it will product the desired
results?

Thanks in advance.

Paul


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default problem with an iif() function in a formula

I just figured out where my problem is, but I still don't know what to do
about it.

I showed the code in my previous post:

Sub enterTP1
numRecords = Range("A1").CurrentRegion.Rows.Count
ActiveSheet.Range(Cells(2, 5), Cells(numRecords, 5)) =
IIf(Range("A1").Cells(2, 4) 0.1, "REG", "")
End Sub

The problem is with the criteria in the iif function:

iif(Range("A1").Cells(2, 4) 0.1 . . .

I realize now that this expression only checks the value of the first cell
in in row 2 of colum D, but I need it to check the value of the cells in the
other rows in column D as it inserts the values in each corresponding row in
column E.

How can I modify my code so it will accomplish this?

Thanks in advance.

Paul


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default problem with an iif() function in a formula

Here are a couple of ways to do this.

The first is to put a formula in the cells that checks the cell in the same row, column D,
calculate those formulas, then replace them with their values.

Sub EnterTP1()
With Range("E2").Resize(Range("A1").CurrentRegion.Rows. Count - 1, 1)
.Formula = "=IF(D20.1,""REG"","""")"
.Calculate
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
End Sub

The second is to retrieve the values from column D into an array, check them one at a time and
put the right result into a second array, then dump that second array into column E.

Sub EnterTP1()
Dim CheckCells As Variant
Dim N As Long
Dim R As Long
Dim Rng As Range
Dim Results As Variant

Set Rng = Range("A1").CurrentRegion
CheckCells = Rng.Columns(4).Value 'put values from column D into an array
N = UBound(CheckCells, 1)
ReDim Results(2 To N, 1 To 1)

For R = 2 To N
If CheckCells(R, 1) 0.1 Then Results(R, 1) = "REG"
Next R

Rng.Cells(2, 5).Resize(N - 1, 1).Value = Results
End Sub

I don't know which of these is faster, but for a relatively small number of rows it shouldn't
matter much.

On Fri, 25 Jul 2003 19:36:38 -0700, "Paul James" wrote:

I just figured out where my problem is, but I still don't know what to do
about it.

I showed the code in my previous post:

Sub enterTP1
numRecords = Range("A1").CurrentRegion.Rows.Count
ActiveSheet.Range(Cells(2, 5), Cells(numRecords, 5)) =
IIf(Range("A1").Cells(2, 4) 0.1, "REG", "")
End Sub

The problem is with the criteria in the iif function:

iif(Range("A1").Cells(2, 4) 0.1 . . .

I realize now that this expression only checks the value of the first cell
in in row 2 of colum D, but I need it to check the value of the cells in the
other rows in column D as it inserts the values in each corresponding row in
column E.

How can I modify my code so it will accomplish this?

Thanks in advance.

Paul


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default problem with an iif() function in a formula

Interestingly enough (at least to me), the 2nd routine is faster by a factor of about 1.6 when
the range is 1000 rows, 2.6 times faster with 18 rows.


On Fri, 25 Jul 2003 22:20:20 -0500, Myrna Larson wrote:

Here are a couple of ways to do this.

The first is to put a formula in the cells that checks the cell in the same row, column D,
calculate those formulas, then replace them with their values.

Sub EnterTP1()
With Range("E2").Resize(Range("A1").CurrentRegion.Rows. Count - 1, 1)
.Formula = "=IF(D20.1,""REG"","""")"
.Calculate
.Copy
.PasteSpecial Paste:=xlPasteValues
End With
End Sub

The second is to retrieve the values from column D into an array, check them one at a time and
put the right result into a second array, then dump that second array into column E.

Sub EnterTP1()
Dim CheckCells As Variant
Dim N As Long
Dim R As Long
Dim Rng As Range
Dim Results As Variant

Set Rng = Range("A1").CurrentRegion
CheckCells = Rng.Columns(4).Value 'put values from column D into an array
N = UBound(CheckCells, 1)
ReDim Results(2 To N, 1 To 1)

For R = 2 To N
If CheckCells(R, 1) 0.1 Then Results(R, 1) = "REG"
Next R

Rng.Cells(2, 5).Resize(N - 1, 1).Value = Results
End Sub

I don't know which of these is faster, but for a relatively small number of rows it shouldn't
matter much.

On Fri, 25 Jul 2003 19:36:38 -0700, "Paul James" wrote:

I just figured out where my problem is, but I still don't know what to do
about it.

I showed the code in my previous post:

Sub enterTP1
numRecords = Range("A1").CurrentRegion.Rows.Count
ActiveSheet.Range(Cells(2, 5), Cells(numRecords, 5)) =
IIf(Range("A1").Cells(2, 4) 0.1, "REG", "")
End Sub

The problem is with the criteria in the iif function:

iif(Range("A1").Cells(2, 4) 0.1 . . .

I realize now that this expression only checks the value of the first cell
in in row 2 of colum D, but I need it to check the value of the cells in the
other rows in column D as it inserts the values in each corresponding row in
column E.

How can I modify my code so it will accomplish this?

Thanks in advance.

Paul


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default problem with an iif() function in a formula

Myrna - thanks so much for the two solutions to my problem. I'm learning a
lot from looking at the code you wrote - the use of the Resize property, the
use of double quotes and the array. Thanks for taking the time to provide
such helpful information.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default problem with an iif() function in a formula

You're welcome. Post back if there is something you don't understand and/or need more help.

On Sat, 26 Jul 2003 15:45:01 GMT, "Paul James" wrote:

Myrna - thanks so much for the two solutions to my problem. I'm learning a
lot from looking at the code you wrote - the use of the Resize property, the
use of double quotes and the array. Thanks for taking the time to provide
such helpful information.


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 / Function Problem TommoUK Excel Discussion (Misc queries) 5 March 25th 08 05:05 AM
Simple problem, simple formula, no FUNCTION ! Ron@Buy Excel Worksheet Functions 6 September 28th 07 04:51 PM
Function problem PE Excel Discussion (Misc queries) 2 April 19th 07 03:36 PM
Problem with IF function.... neilcarden Excel Worksheet Functions 2 March 27th 07 04:32 PM
Problem with IF function [email protected] Excel Discussion (Misc queries) 5 January 19th 06 04:11 PM


All times are GMT +1. The time now is 05:51 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"