# VBA Help

**URL:** <https://forums.speedlife.net/t/vba-help/207950>\
**Category:** NYSpeed Off Topic\
**Created:** [January 7, 2011, 10:47am UTC](https://forums.speedlife.net/t/vba-help/207950 "2011-01-07T10:47:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![travisn](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/travisn/32/5046_2.png) [@travisn](https://forums.speedlife.net/u/travisn)\
**Post date:** [January 7, 2011, 10:47am UTC](https://forums.speedlife.net/t/vba-help/207950/1 "2011-01-07T10:47:55Z")

</div>

I cant get the number 2 to display properly.

Problem: Write a VBA function to solve for f(x) for different values of x ranging from 0 to 2 with increments of 0.1.

I called a cell I for increment of .1, then used “=cell+i” for 0-2.1. For some reason 2 displays as out of range. I was told its because of ‘phantom digits’ when using non integer values and a solution is to use for loops and integers with division. There has to be a solution using if statements though.

Function hw\_3(x)

If x \>= 0 And x \<= 0.5 Then  
hw\_3 = 1.2 \* x ^ 2  
ElseIf x \> 0.5 And x \< 1.2 Then  
hw\_3 = 1 - ((1 / 12) \* (x - 0.5))  
ElseIf x \>= 1.2 And x \<= 2 Then  
hw\_3 = Exp(-2 \* x)  
ElseIf x \< 0 Or x \> 2 Then  
hw\_3 = “out of range”  
End If

End Function

Any ideas? I can submit it as is since its a known issue so I’m just looking for an solution out of curiosity.

---------- Post added at 01:47 PM ---------- Previous post was at 01:42 PM ----------

I wrote it with an if statement inside of a cell and it worked. wtf.

=IF(AND(B19\>=0,B19\<=0.5),1.2\*(B19)^2,(IF(AND(B19\>0.5,B19\<1.2),1-(1/12)_(B19-0.5),IF(AND(B19\>=1.2,B19\<=2),EXP(-2_B19),“out of range”))))

---

<div class="post-metadata">

**Author:** ![ProgRocker](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/progrocker/32/5434_2.png) [@ProgRocker](https://forums.speedlife.net/u/ProgRocker)\
**Post date:** [January 7, 2011, 12:18pm UTC](https://forums.speedlife.net/t/vba-help/207950/2 "2011-01-07T12:18:23Z")

</div>

What if you try adding:

ElseIf x = 2 Then  
hw\_3 = Exp(-2 \* x)

I’m no programmer, just throwing it out there.

---

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [January 7, 2011, 12:23pm UTC](https://forums.speedlife.net/t/vba-help/207950/3 "2011-01-07T12:23:54Z")

</div>

what happens with .2?

---

<div class="post-metadata">

**Author:** ![ProgRocker](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/progrocker/32/5434_2.png) [@ProgRocker](https://forums.speedlife.net/u/ProgRocker)\
**Post date:** [January 7, 2011, 12:39pm UTC](https://forums.speedlife.net/t/vba-help/207950/4 "2011-01-07T12:39:20Z")

</div>

> [@boardjnky4](#):
>
> what happens with .2?

Should equal 0.048 correct?

---

<div class="post-metadata">

**Author:** ![boardjnky4](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@boardjnky4](https://forums.speedlife.net/u/boardjnky4)\
**Post date:** [January 7, 2011, 12:52pm UTC](https://forums.speedlife.net/t/vba-help/207950/5 "2011-01-07T12:52:42Z")

</div>

well, I don’t realy care what the end result is…The way he defined the condition in the first part is basically the same as the part he’s having trouble with. Just wanted to make sure that was working.

---------- Post added at 03:52 PM ---------- Previous post was at 03:51 PM ----------

actually, what I really meant, is what happens with .5?

---

<div class="post-metadata">

**Author:** ![travisn](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/travisn/32/5046_2.png) [@travisn](https://forums.speedlife.net/u/travisn)\
**Post date:** [January 7, 2011, 2:52pm UTC](https://forums.speedlife.net/t/vba-help/207950/6 "2011-01-07T14:52:34Z")

</div>

> [@ProgRocker](#):
>
> What if you try adding:
> 
> ElseIf x = 2 Then  
> hw\_3 = Exp(-2 \* x)
> 
> I’m no programmer, just throwing it out there.

I tried it, doesn’t change anything

> [@boardjnky4](#):
>
> what happens with .2?

see picture

> [@boardjnky4](#):
>
> actually, what I really meant, is what happens with .5?

.5 is caught with the first line since its \<= .5 (which means less than or equal to) but NOT the second line because it is just \<.5

---

<div class="post-metadata">

**Author:** ![ProgRocker](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/progrocker/32/5434_2.png) [@ProgRocker](https://forums.speedlife.net/u/ProgRocker)\
**Post date:** [January 7, 2011, 4:43pm UTC](https://forums.speedlife.net/t/vba-help/207950/7 "2011-01-07T16:43:30Z")

</div>

what about changing:

ElseIf x \< 0 Or x \> **2.1** Then  
hw\_3 = “out of range”
