[mso] Re: Visual Basic in Excel

  • From: "David Smart" <smartware@xxxxxxxxxxxxxxx>
  • To: <mso@xxxxxxxxxxxxx>
  • Date: Fri, 13 Jul 2007 23:13:30 +1000

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

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

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

You should not be using this old form of the If statement.  Please always 
end your line at "Then", put the statement on the next line, and use an End 
If with EVERY If statement.  (Or at least do this until you are EXTREMELY 
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 
objecting to your first ElseIf.

You've also shown your cell formula as

  = 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 
the time.  :-(  Remove it too.

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

Regards, Dave S

----- Original Message ----- 
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 
> 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
>
> = Discount (B4,C4)
>
> My VBA forst draft is below...
>
> Function Discount(Customer_Type As String, Quantity_of_bks As Integer)
> If Customer_Type = "Individual" And Quantity_of_bks < 5 Then Discount = 0#
>                                If Quantity_of_bks >= 5 And Quantity_of_bks
> < 24 Then Discount = 0.15
>                                If Quantity_of_bks > 25 Then Discount = 
> 0.25
> ElseIf Quanitity_of_bks < 5 Then Discount = 0.05
>    If Quanitity_of_bks >= 5 < 14 Then Discount = 0.1
>    If Quantity_of_bks >= 15 < 24 Then Discount = 0.15
>    If Quantity_of_bks >= 25 < 49 Then Discount = 0.2
>    If Quantity_of_bks > 50 Then Discount = 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 = "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 
> 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 
> 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: