+ Reply to Thread
Results 1 to 5 of 5

Help with a code in userform

  1. #1
    Registered User
    Join Date
    11-06-2013
    Location
    Puerto Rico
    MS-Off Ver
    Excel 2010
    Posts
    2

    Help with a code in userform

    This program is for a homework and I have most part of it till the part where I have to add $2 or $1 if the hours per months passed the limit. Can someone help me with this code?

    This is the code I have till now:

    Private Sub Calculate_Click()

    'Variables
    Dim MC As Double
    Dim lblMC As Currency

    'Input Validation
    If txtHC.Value = "" Then
    txtHC = InputBox("Please enter a number of hours")
    ElseIf txtHC > 744 Then
    txtHC = InputBox("Hours of Access cannot be higher than 744 hours")
    End If

    'Process
    If radA.Value = True Then
    MC = 9.95
    ElseIf radB.Value = True Then
    MC = 14.95
    ElseIf radC.Value = True Then
    MC = 19.95
    End If
    Screenshot_2013-11-06-19-17-22-1.jpg

  2. #2
    Forum Contributor
    Join Date
    03-21-2013
    Location
    Corvallis, OR
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Help with a code in userform

    Assuming that radA,B,C are defined elsewhere, where is the definition of "hours per month passed the limit"?

    I'm guessing that your radA values determine whether to use $1 or $2... In which case you could define which $ amount to use based on the radA values first in a similar series of if statements that you are using to define MC. Lets say you use Xtra as the extra dollar amount to add to MC. The following code would determine the amount to use if the hours limit is passed.
    Please Login or Register  to view this content.
    Then you could use another if statement at the end which adds the Xtra value if the time limit is passed; something like:
    Please Login or Register  to view this content.
    This would add the predetermined extra $1 or $2 to the MC amount. Does that help?

  3. #3
    Registered User
    Join Date
    11-06-2013
    Location
    Puerto Rico
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Help with a code in userform

    Variables:
    txtHC = textbox Hours of Access (Input)
    radA = Package A
    radB = Package B
    radC = Package C
    MC = Mounthly Charge (Output)
    • On Package A the charge is $9.95 for 10 hours of access plus $2 for each addicional hour.
    • On Package B the charge is $14.95 for 20 hours of access plus $1 for each addicional hour.
    • On Package A the charge is $19.95 for unlimited hours of access.

    There's also a checkbox to check if the client is from a NonProfit Organization that gives the client 20% of discount on all the packages. The user must enter the hours of access, select a package and the check the checkbox if the client is from a NonProfit Organization or leave it blank if is not. The Output must be the monthly charge.

    I don't know how to make possible to add $2 or $1 depending on the package and the hours of access. For example:

    Client 1: Package A, 5 hours, nonprofit $7.96
    Client 2: Package B, 25 hours $19.95

    On the client 2 it has 25 hours of access and it was selected package B meaning that there are 5 hours of access addionals that must be charge $1 each but I don't know how to do that or the 20% of discount.

  4. #4
    Forum Contributor
    Join Date
    03-21-2013
    Location
    Corvallis, OR
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Help with a code in userform

    This all sounds like something better handled by formulas on a spreadsheet (hidden if necessary) that base their calculations on inputs from your userform.
    You could use multiple if statements in your VBA to figure out which package is selected, then based on that, if the hours are over the amount for the selected package, take the difference and multiply by the extra value for that package and add it to the regular package value per hour to determine the MC value. It would look something like this:
    Please Login or Register  to view this content.
    If the $ amounts are not hourly, and only change based on hours beyond 10 or 20, then remove the /10 and /20 from the if statements to keep the values complete until the max hours are passed.
    Last edited by bmxfreedom; 11-07-2013 at 08:28 PM. Reason: added rounding to the calculations, and added userform appropriate code (me.objectname.value)

  5. #5
    Forum Contributor
    Join Date
    03-21-2013
    Location
    Corvallis, OR
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Help with a code in userform

    Also, if this is an actual userform, the code changes a bit. It's best to specify that the objects (like radA) are part of the userform with the phrase Me, like this: Me.radA. Also, you can't refer to radA as a value, you have to refer to the value of radA as an object; like this: Me.radA.value

    Without knowing what your checkbox is called, I can't write code that would apply the 20% off for non-profit, but that should be an easy step later in the code; something like this:
    Please Login or Register  to view this content.
    Last edited by bmxfreedom; 11-07-2013 at 08:29 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Userform code fails when the userform is called from a custom ribbon button
    By klonbeck in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2013, 03:00 PM
  2. [SOLVED] VB Code to Call Userform in Userform
    By Stonesteel15 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 03-22-2012, 04:47 AM
  3. Userform code
    By ytrehguodleinad in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-18-2011, 10:59 AM
  4. Help with Userform Code
    By JessFace in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2010, 09:57 AM
  5. UserForm Code
    By barkerx5 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-25-2008, 02:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1