[mso] Re: Visual Basic in Excel

  • From: "Tillotson, Robert N \(US SSA\)" <robert.tillotson@xxxxxxxxxxxxxx>
  • To: <mso@xxxxxxxxxxxxx>
  • Date: Fri, 13 Jul 2007 13:16:34 -0400

In addition to David's great suggestions, you need to revise the test =
cases for the Ifs.

If Quanitity_of_bks >=3D 5 < 14  is not interpreted as you mean.

You mean If (Quanitity_of_bks >=3D 5 ) and (Quanitity_of_bks < 14) then =
...
for what you typed the 5 < 14 is evaluated as True and any quantity will =
match that.  You aren't actually testing the numbers with this =
statement.

You could make this more efficient by using case statements with the IF =
customer is individual if test.  Especially if you are using this in a =
large spreadsheet, going through each IF for every cell will take a long =
time.

There are other issues to use a VBA function in a spreadsheet as well as =
using the volatile keyword so that your functions recalculate with =
changes, but get the function working right in the VBE first then you =
can address making it work right  worksheet.

Robert Tillotson


-----Original Message-----
From: mso-bounce@xxxxxxxxxxxxx [mailto:mso-bounce@xxxxxxxxxxxxx]On
Behalf Of David Smart
Sent: Friday, July 13, 2007 9:14 AM
To: mso@xxxxxxxxxxxxx
Subject: [mso] Re: Visual Basic in Excel


Please insert an "Option Explicit" as the very top line of your module =
(i.e.=20
before the Function statement).  You have a typo that is causing an =
unwanted=20
variable to be defined.

Quanitity_of_bks   instead of   Quantity_of_bks    (i.e. there is an =
extra I=20
in there)    there are two of these.  Option Explicit will get Excel to =
find=20
these.

Also, can I assume that you have statements to the right of the Then=20
keywords (i.e. on the same line as the Then)?  If you do, then you =
certainly=20
can't also have ElseIf clauses, or even End If.

You should not be using this old form of the If statement.  Please =
always=20
end your line at "Then", put the statement on the next line, and use an =
End=20
If with EVERY If statement.  (Or at least do this until you are =
EXTREMELY=20
familiar with writing successful VBA code.  After that, you can start to =

take shortcuts if you like.)

Do a Compile of the code (from the Debug menu) and you'll find that =
Excel is=20
objecting to your first ElseIf.

You've also shown your cell formula as

  =3D Discount (B4,C4)

You must not have a space before the opening parenthesis.

The space after the equals sign is also going to cause trouble - but not =
all=20
the time.  :-(  Remove it too.

Lastly, I note you have a constant 0#.  This is obsolete - remove the =
hash=20
sign.

Regards, Dave S

----- Original Message -----=20
From: "Rachel Jobes" <racheljobes@xxxxxxxxx>
To: <mso@xxxxxxxxxxxxx>
Sent: Friday, July 13, 2007 5:09 AM
Subject: [mso] Visual Basic in Excel


> Hi All
> I am just trying to write my first bit of VBA and have found it quite =
easy
> however I have then come to input the formula in my cell and it doesnt =

> work.
>
> I have created a lot of variations within the VBA saving each version =
but=20
> I
> always get a error usually #REF #VALUE or #NAME - when these appear I =
use
> the excel help with no avail.
>
> My spreadsheet is customer type column b and quanitity of books column =
c
>
> My function / formulae is being entered into column D
>
> =3D Discount (B4,C4)
>
> My VBA forst draft is below...
>
> Function Discount(Customer_Type As String, Quantity_of_bks As Integer)
> If Customer_Type =3D "Individual" And Quantity_of_bks < 5 Then =
Discount =3D 0#
>                                If Quantity_of_bks >=3D 5 And =
Quantity_of_bks
> < 24 Then Discount =3D 0.15
>                                If Quantity_of_bks > 25 Then Discount =
=3D=20
> 0.25
> ElseIf Quanitity_of_bks < 5 Then Discount =3D 0.05
>    If Quanitity_of_bks >=3D 5 < 14 Then Discount =3D 0.1
>    If Quantity_of_bks >=3D 15 < 24 Then Discount =3D 0.15
>    If Quantity_of_bks >=3D 25 < 49 Then Discount =3D 0.2
>    If Quantity_of_bks > 50 Then Discount =3D 0.3
> End If
> End Function
>
> Things I have tried include an End IF after the first IF and an END If =
at
> after every line I have also tried writing in the other customer types
> options so instead of having an Else IF I have another IF e.g. If
> Customer_Type =3D "Educational Inst" or "Library" or "School" And =
.......
> Nothing seemed to create a result when the function is input in the =
cell.
>
> I am not sure whether my VBA is incorrect or the Formulae.  I dont =
even=20
> mind
> if I get an incorrect answer I can amend the VBA but I cant get past =
the
> errors.
>
> Can anyone help?
> Thanks
> Rache
>
>
> *************************************************************
> You are receiving this mail because you subscribed to =
mso@xxxxxxxxxxxxx or=20
> MicrosoftOffice@xxxxxxxxxxxxxxxx
>
> To send mail to the group, simply address it to mso@xxxxxxxxxxxxx
>
> To Unsubscribe from this group, visit the group's homepage and use the =

> dropdown menu at the top.  This will allow you to unsubscribe your =
email=20
> address or change your email settings to digest or vacation (no mail).
> //www.freelists.org/webpage/mso
>
> To be able to share files with the group, you must join our Yahoo =
sister=20
> group.  This group will not allow for posting of emails, but will =
allow=20
> you to join and share problem files, templates, etc.:=20
> http://tech.groups.yahoo.com/group/MicrosoftOffice . This group is for =

> FILE SHARING ONLY.
>
> If you are using Outlook and you see a lot of unnecessary code in your =

> email messages, read these instructions that explain why and how to =
fix=20
> it:
> http://personal-computer-tutor.com/abc3/v28/greg28.htm
> *************************************************************
>=20

*************************************************************
You are receiving this mail because you subscribed to mso@xxxxxxxxxxxxx =
or MicrosoftOffice@xxxxxxxxxxxxxxxx

To send mail to the group, simply address it to mso@xxxxxxxxxxxxx

To Unsubscribe from this group, visit the group's homepage and use the =
dropdown menu at the top.  This will allow you to unsubscribe your email =
address or change your email settings to digest or vacation (no mail).
//www.freelists.org/webpage/mso

To be able to share files with the group, you must join our Yahoo sister =
group.  This group will not allow for posting of emails, but will allow =
you to join and share problem files, templates, etc.:  =
http://tech.groups.yahoo.com/group/MicrosoftOffice . This group is for =
FILE SHARING ONLY.

If you are using Outlook and you see a lot of unnecessary code in your =
email messages, read these instructions that explain why and how to fix =
it:
http://personal-computer-tutor.com/abc3/v28/greg28.htm
*************************************************************
*************************************************************
You are receiving this mail because you subscribed to mso@xxxxxxxxxxxxx or 
MicrosoftOffice@xxxxxxxxxxxxxxxx

To send mail to the group, simply address it to mso@xxxxxxxxxxxxx

To Unsubscribe from this group, visit the group's homepage and use the dropdown 
menu at the top.  This will allow you to unsubscribe your email address or 
change your email settings to digest or vacation (no mail).
//www.freelists.org/webpage/mso

To be able to share files with the group, you must join our Yahoo sister group. 
 This group will not allow for posting of emails, but will allow you to join 
and share problem files, templates, etc.:  
http://tech.groups.yahoo.com/group/MicrosoftOffice . This group is for FILE 
SHARING ONLY.

If you are using Outlook and you see a lot of unnecessary code in your email 
messages, read these instructions that explain why and how to fix it:
http://personal-computer-tutor.com/abc3/v28/greg28.htm
*************************************************************

Other related posts: