Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default using decimal numbers in for-next statement

I'm using a for-next statement for finding values in certain ranges.

When I use whole numbers, there is no problem and everything work
allright. But in one range I need to beable to use decimal numbers, an
suddenly my code seems to contain errors. Here is the working code:

If Not pro1.text = "" Then
fa = pro1.text
fb = pro2.text
For x = fa To fb
For y = 1 To 5
Set c = Range("F2:F2000").Find(fa, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 7
c.Offset(0, -1).Interior.ColorIndex = 7
c.Offset(0, -2).Interior.ColorIndex = 7
c.Offset(0, -3).Interior.ColorIndex = 7
c.Offset(0, -4).Interior.ColorIndex = 7
c.Offset(0, -5).Interior.ColorIndex = 7
End If
y = y + 1
Next y
fa = fa + 1
Next x
End If

And the code that doesn't work:

If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb
For y = 1 To 5
Set c = Range("G2:G2000").Find(ga, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
y = y + 1
Next y
ga = ga + 0.01
Next x
End If

As you can clearly see, the only thing I've changed is the interval fo
the next value of x.
I've spend the last three hours coming up with solutions, but nothin
is working.
I'm hoping one of you can see one?!

tnx in advance,
Pee

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 292
Default using decimal numbers in for-next statement

Hi

Intervals should not be set and inside a a loop, and counter values should
not be changed inside a a loop, provide everything in the For statement:

Sub TestLoops()
Dim D As Double
Dim L As Long

For D = 0.1 To 1.3 Step 0.1
For L = 1 To 3
Debug.Print D, L
Next
Next
End Sub

HTH. best wishes Harald

"Peer " skrev i melding
...
I'm using a for-next statement for finding values in certain ranges.

When I use whole numbers, there is no problem and everything works
allright. But in one range I need to beable to use decimal numbers, and
suddenly my code seems to contain errors. Here is the working code:

If Not pro1.text = "" Then
fa = pro1.text
fb = pro2.text
For x = fa To fb
For y = 1 To 5
Set c = Range("F2:F2000").Find(fa, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 7
c.Offset(0, -1).Interior.ColorIndex = 7
c.Offset(0, -2).Interior.ColorIndex = 7
c.Offset(0, -3).Interior.ColorIndex = 7
c.Offset(0, -4).Interior.ColorIndex = 7
c.Offset(0, -5).Interior.ColorIndex = 7
End If
y = y + 1
Next y
fa = fa + 1
Next x
End If

And the code that doesn't work:

If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb
For y = 1 To 5
Set c = Range("G2:G2000").Find(ga, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
y = y + 1
Next y
ga = ga + 0.01
Next x
End If

As you can clearly see, the only thing I've changed is the interval for
the next value of x.
I've spend the last three hours coming up with solutions, but nothing
is working.
I'm hoping one of you can see one?!

tnx in advance,
Peer


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default using decimal numbers in for-next statement

Not sure your code is working as you think?

ga = 10
gb = 12
for x = ga to gb : debug.Print x: ga = ga + 0.1 : next
10
11
12

perhaps you want

for x = ga to gb step 0.01

for x = ga to gb step 0.1 : debug.Print x : next
10.3
10.4
10.5
10.6
10.7
10.8
10.9
11
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9
12

--
Regards,
Tom Ogilvy



"Peer " wrote in message
...
I'm using a for-next statement for finding values in certain ranges.

When I use whole numbers, there is no problem and everything works
allright. But in one range I need to beable to use decimal numbers, and
suddenly my code seems to contain errors. Here is the working code:

If Not pro1.text = "" Then
fa = pro1.text
fb = pro2.text
For x = fa To fb
For y = 1 To 5
Set c = Range("F2:F2000").Find(fa, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 7
c.Offset(0, -1).Interior.ColorIndex = 7
c.Offset(0, -2).Interior.ColorIndex = 7
c.Offset(0, -3).Interior.ColorIndex = 7
c.Offset(0, -4).Interior.ColorIndex = 7
c.Offset(0, -5).Interior.ColorIndex = 7
End If
y = y + 1
Next y
fa = fa + 1
Next x
End If

And the code that doesn't work:

If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb
For y = 1 To 5
Set c = Range("G2:G2000").Find(ga, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
y = y + 1
Next y
ga = ga + 0.01
Next x
End If

As you can clearly see, the only thing I've changed is the interval for
the next value of x.
I've spend the last three hours coming up with solutions, but nothing
is working.
I'm hoping one of you can see one?!

tnx in advance,
Peer


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default using decimal numbers in for-next statement

tnx, guys

But it still won't work!

Even using for..step..next the whole numbers work, and decimal number
won't!
Maybe it has something to do with my country settings. I'm living i
holland and we write decimal numbers like this:
xx,yy (10,00 for example)
But I tried using country settings for UK (10.00 for example) - n
effect!
Any other suggestions?

P.S. now my code is:
If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb Step 0.01
Debug.Print x
For y = 1 To 5 Step 1
Set c = Range("G2:G500").Find(x, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
Next y
Next x
End I

--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default using decimal numbers in for-next statement

Hi

Which datatypes are ga and gb declared as ? I'd think the problems lie he
ga = ppe1.text
gb = ppe2.text
does it work with

For x = 9 To 11 Step 0.01

? Also, turn off any error handlers (like on error resume next) and see what
happens.

Best wishes Harald

"Peer " skrev i melding
...
tnx, guys

But it still won't work!

Even using for..step..next the whole numbers work, and decimal numbers
won't!
Maybe it has something to do with my country settings. I'm living in
holland and we write decimal numbers like this:
xx,yy (10,00 for example)
But I tried using country settings for UK (10.00 for example) - no
effect!
Any other suggestions?

P.S. now my code is:
If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb Step 0.01
Debug.Print x
For y = 1 To 5 Step 1
Set c = Range("G2:G500").Find(x, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
Next y
Next x
End If


---
Message posted from http://www.ExcelForum.com/





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default using decimal numbers in for-next statement

in addition to Harald's advice, decimal numbers are not always stored
exactly. For example, if you format the cell for two decimal places, it
might appear as
2.43

but actually be stored as 2.429999999 (same type thing if the value is
produced by a formula).

So there may not actually be an exact match.

--
Regards,
Tom Ogilvy

"Peer " wrote in message
...
tnx, guys

But it still won't work!

Even using for..step..next the whole numbers work, and decimal numbers
won't!
Maybe it has something to do with my country settings. I'm living in
holland and we write decimal numbers like this:
xx,yy (10,00 for example)
But I tried using country settings for UK (10.00 for example) - no
effect!
Any other suggestions?

P.S. now my code is:
If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb Step 0.01
Debug.Print x
For y = 1 To 5 Step 1
Set c = Range("G2:G500").Find(x, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
Next y
Next x
End If


---
Message posted from http://www.ExcelForum.com/



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default using decimal numbers in for-next statement

I have tried every datatype it could possibly be;

integer, long, single, double, even variant after which I define
decimal numbers with cdec.

It was the first solution (or error) I thought of.

And the numbers are exactly the numbers I'm searching for, beause ther
are no formula's in the range, and the values are manual input.

:confused

--
Message posted from http://www.ExcelForum.com

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default using decimal numbers in for-next statement

Can you find the values manually using Edit=find. If so, then you should
be able to do it with the Find method. If you can't, then you probably
can't with the find method either. cdec is certainly on the answer.

--
Regards,
Tom Ogilvy

"Peer " wrote in message
...
I have tried every datatype it could possibly be;

integer, long, single, double, even variant after which I defined
decimal numbers with cdec.

It was the first solution (or error) I thought of.

And the numbers are exactly the numbers I'm searching for, beause there
are no formula's in the range, and the values are manual input.




---
Message posted from http://www.ExcelForum.com/



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default using decimal numbers in for-next statement

Tom's comment about precision loss in cells also applies to VBA. Try
this simple test. In the Immediate Window type in

for i=2.00 to 3.00 step 0.01:debug.Print i:next i

Upto 2.23 everything is as expected. Then, instead of 2.24, I get
2.23999999999999. By contrast, the following gave accurate results
from 2 to 3:

For xStep=0 to (3-2)/0.01:x=2+xStep*0.01:debug.Print x:next xstep

So, you may want to try:

dim xStep as integer,x as double
'might need long rather than integer
For xStep=0 to (gb-ga)/0.01
x=ga+xStep*0.01
...
next xStep

Also, when you share code, it would help if you included the variable
declaration statements.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article , Peer
says...
I have tried every datatype it could possibly be;

integer, long, single, double, even variant after which I defined
decimal numbers with cdec.

It was the first solution (or error) I thought of.

And the numbers are exactly the numbers I'm searching for, beause there
are no formula's in the range, and the values are manual input.




---
Message posted from http://www.ExcelForum.com/


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default using decimal numbers in for-next statement

Set c = Range("G2:G500").Find(x, ActiveCell,...

Is "ActiveCell" within the range of G2:G500?

Consider the "Currency" data type also when working with numbers with 2
decimal places.

Dana DeLouis

"Peer " wrote in message
...
tnx, guys

But it still won't work!

Even using for..step..next the whole numbers work, and decimal numbers
won't!
Maybe it has something to do with my country settings. I'm living in
holland and we write decimal numbers like this:
xx,yy (10,00 for example)
But I tried using country settings for UK (10.00 for example) - no
effect!
Any other suggestions?

P.S. now my code is:
If Not ppe1.text = "" Then
ga = ppe1.text
gb = ppe2.text
For x = ga To gb Step 0.01
Debug.Print x
For y = 1 To 5 Step 1
Set c = Range("G2:G500").Find(x, ActiveCell, xlValues, _
xlWhole, xlByColumns, xlNext)
If Not c Is Nothing Then
c.Activate
c.Interior.ColorIndex = 8
c.Offset(0, -1).Interior.ColorIndex = 8
c.Offset(0, -2).Interior.ColorIndex = 8
c.Offset(0, -3).Interior.ColorIndex = 8
c.Offset(0, -4).Interior.ColorIndex = 8
c.Offset(0, -5).Interior.ColorIndex = 8
c.Offset(0, -6).Interior.ColorIndex = 8
End If
Next y
Next x
End If


---
Message posted from http://www.ExcelForum.com/





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
Decimal Numbers typed into Excel 2003 read as whole numbers john mcmichael Excel Discussion (Misc queries) 1 May 10th 07 08:18 PM
Subtracting two 2-decimal place numbers gives result 13-decimal places? [email protected] Excel Worksheet Functions 5 March 12th 07 10:38 PM
If then statement displaying 2 or 3 decimal places Miles Excel Worksheet Functions 2 February 27th 07 12:44 AM
How can I sort mostly 3 decimal with some 4 decimal numbers PeterM Excel Discussion (Misc queries) 4 August 16th 06 02:15 AM
If Statement - in time not decimal minutes Sue Excel Worksheet Functions 1 March 30th 05 04:35 PM


All times are GMT +1. The time now is 02:12 PM.

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

About Us

"It's about Microsoft Excel"