I'm trying to add labels together in a list box and they add together just fine with val(). However when I add currency signs to the label val() stops working.
This is the code that works without using any currency signs:
'Sales Tax
Public Const intSALESTAX_Rate As Decimal = 0.06
'Shipping cost
Public Const Shipping_Rate As Integer = 2
'Audio Books
Public Const History_of_Scotland_Audio As Decimal = 14.5
Public Const Calculus_One_Day_Audio As Decimal = 29.95
Public Const Science_Body_Language_Audio As Decimal = 12.95
Public Const Relaxation_Techniques_Audio As Decimal = 11.5
'Print Books
Public Const Did_Your_Way_Print As Decimal = 11.95
Public Const History_Of_Scotland_Print As Decimal = 14.5
Public Const Calculus_One_Day_Print As Decimal = 29.95
Public Const Feel_The_Stress_Print As Decimal = 18.5
Private Sub btnAddtoCartAudioBook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddtoCartAudioBook.Click
Dim value As Decimal
Dim shipping As Integer
Dim tax As Decimal
Dim grand_total As Decimal
Dim ShoppingCart As New Form1()
If lstAudioBooks.SelectedIndex = -1 Then
MessageBox.Show("Select an audio book.")
Return
End If
Form1.lstShoppingCart.Items.Add(lstAudioBooks.SelectedItem.ToString())
Select Case lstAudioBooks.SelectedItem.ToString()
Case "Learn Calculus in One Day (Audio)"
Debug.WriteLine("Learn Calculus in One Day (Audio)")
value = Val(Form1.lblSubtotal.Text) + Form1.Calculus_One_Day_Audio
Form1.lblSubtotal.Text = FormatNumber(value, 2)
Case "Relaxation Techniques (Audio)"
Debug.WriteLine("Relaxation Techniques (Audio)")
value = Val(Form1.lblSubtotal.Text) + Form1.Relaxation_Techniques_Audio
Form1.lblSubtotal.Text = FormatNumber(value, 2)
Case "The History of Scotland (Audio)"
Debug.WriteLine("The History of Scotland (Audio)")
value = Val(Form1.lblSubtotal.Text) + Form1.History_of_Scotland_Audio
Form1.lblSubtotal.Text = FormatNumber(value, 2)
Case "The Science of Body Language (Audio)"
Debug.WriteLine("The Science of Body Language (Audio)")
value = Val(Form1.lblSubtotal.Text) + Form1.Science_Body_Language_Audio
Form1.lblSubtotal.Text = FormatNumber(value, 2)
Case Else
Debug.WriteLine("No Value")
End Select
'shipping calculations
shipping = Val(Form1.lblShipping.Text) + Form1.Shipping_Rate
Form1.lblShipping.Text = FormatNumber(shipping, 2)
'tax calculations
tax = value * Form1.intSALESTAX_Rate
Form1.lblTax.Text = FormatNumber(tax, 2)
'Sub total calculation
grand_total = FormatNumber(value + shipping + tax, 2)
Form1.lblTotal.Text = FormatNumber(grand_total, 2)
Return
End Sub
However if I add a dollar sign to the label val() stops working
value = Val(Form1.lblSubtotal.Text) + Form1.Calculus_One_Day_Audio
Form1.lblSubtotal.Text = "$" + FormatNumber(value, 2)
How can I run calculations with the labels if the value contains a dollar sign?
one method would be to remove the dollar sign before calling the val() function. For example lets say the numbers are in this format:
$45, $1, $456
dim strTemp as string
strTemp = strings.right(strings.len(Form1.lblSubtotal.Text)-1)
Value = Val(strTemp)
if its in the format:
$ 45, $ 1, $ 456
dim strTemp as string
strTemp = strings.right(strings.len(Form1.lblSubtotal.Text)-2)
Value = Val(strTemp)
Related
I have 4 textboxes and one label on my form, 4 textboxes represent 4 payment types (cash, card, bank transfer and other payment method).
In my label i have summary value which i increment when i'm adding new item to receipt.
Textbox names are: cashTbox cardTbox bankTbox otherTbox
Label name is: summaryLbl
By default cashTextbox.text is equal to summ.content and every time I add an item to database i increment summaryLbl + itemPrice (item price from database).
I have tried making a function which will allow me to subtract from cashTboxto other payment types without allowing cashTbox to go to negative value.
My code is as follows:
Private Sub cashTbox_LostKeyboardFocus(sender As Object, e As KeyboardFocusChangedEventArgs) Handles cashTbox.LostKeyboardFocus
If sender.IsFocused = True And sender.text.length > 0 Then
If calculateP(sender) = True Then
End If
End If
End Sub
Dim sumP as decimal = summaryLbl.content
Dim cashP as decimal = cashTbox.text
Dim cardP as decimal = cardTbox.text
Dim bankP as decimal = bankTbox.text
Dim otherP as decimal = otherTbox.text
Public Function calculateP(ByVal s As Object)
Try
If String.IsNullOrEmpty(s.text) Then
s.text = "0,00"
Else
s.text = Decimal.Parse(s.text)
End If
If s Is cashTbox Then
ElseIf s Is cardTbox Then
cardP = s.text
cashTbox.Text = sumP - (cardP + bankP + otherP )
ElseIf s Is bankTbox Then
bankP = s.text
cashTbox.Text = sumP - (cardP + bankP + otherP )
ElseIf s Is otherTbox Then
otherP = s.text
cashTbox.Text = sumP - (cardP + bankP + otherP )
End If
cashTbox.Text = sumP - (cardP + bankP + otherP )
Catch ex As Exception
End Try
setValues()
Return True
End Function
Public Function setValues()
summaryLbl.content = sumP
cashTbox.text = cashP
cardTbox.text = cardP
bankTbox.text = bankP
otherTbox.text = otherP
End Function
This resets the text box for quantity and amount to zero when i test, how do i make this calculate?
Dim Number As Integer
txtNumber.Text = Number
Dim orchestra As Integer = 40
Dim mezz As Integer = 27.5
Dim general As Integer = 15
Dim balcony As Integer = 10
If lstSection.SelectedItem = "Orchestra" Then
txtAmount.Text = (Number * 40)
ElseIf lstSection.SelectedItem = "Mezzanine" Then
txtAmount.Text = (Number * 27.5)
ElseIf lstSection.SelectedItem = "General" Then
txtAmount.Text = (Number * 15)
ElseIf lstSection.SelectedItem = "Balcony" Then
txtAmount.Text = (Number * 10)
End If
Assuming my edit is correct, and you intend to declare your Number variable within the same method, the problem you're seeing is occurring because you're not assigning a value to Number.
Integer value types always initialize with the default value of 0.
Thus: txtAmount.Text = (0 * 40), which of course will always be 0.
I would like to make CPU to calculate declared result from the given numbers that are also declared.
So far:
Dim ArrayOperators() As String = {"+", "-", "*", "/", "(", ")"}
Dim GlavniBroj As Integer = GBRnb() 'Number between 1 and 999 that CPU needs to get from the numbers given below:
Dim OsnovniBrojevi() As Integer = {OBRnb(), OBRnb(), OBRnb(), OBRnb()} '4 numbers from 1 to 9
Dim SrednjiBroj As Integer = SBRnb() '1 number, 10, 15 or 20 chosen randomly
Dim KrajnjiBroj As Integer = KBRnb() '25, 50, 75 or 100 are chosen randomly
Private Function GBRnb()
Randomize()
Dim value As Integer = CInt(Int((999 * Rnd()) + 1))
Return value
End Function
Private Function OBRnb()
Dim value As Integer = CInt(Int((9 * Rnd()) + 1))
Return value
End Function
Private Function SBRnb()
Dim value As Integer = CInt(Int((3 * Rnd()) + 1))
If value = 1 Then
Return 10
ElseIf value = 2 Then
Return 15
ElseIf value = 3 Then
Return 20
End If
Return 0
End Function
Private Function KBRnb()
Dim value As Integer = CInt(Int((4 * Rnd()) + 1))
If value = 1 Then
Return 25
ElseIf value = 2 Then
Return 50
ElseIf value = 3 Then
Return 75
ElseIf value = 4 Then
Return 100
End If
Return 0
End Function
Is there any way to make a program to calculate GlavniBroj(that is GBRnb declared) with the help of the other numbers (also without repeating), and with help of the given operators? Result should be displayed in the textbox, in a form of the whole procedure of how computer got that calculation with that numbers and operators. I tried to make it work by coding operations one by one, but that's a lot of writing... I'm not looking exactly for the code answer, but mainly for the coding algorithm. Any idea? Thanks! :)
I need to convert a four digit number to a three digit alphanumeric number where
001-999 = 001-999
1000 = 00A
1027 = 01A
etc.. up to whatever = ZZZ
I am very new to programming and cannot figure out how to proceed with this issue. Any help would be GREATLY appreciated.
How does this work for you?
Private Function ConvertMyNumber(toConvert As UShort) As String
Const MaxValueToConvert = 3885S
If (toConvert > MaxValueToConvert) Then Throw New ArgumentException(String.Format("Argument value of {0} exceeds maximum possible value of {1}.", toConvert, MaxValueToConvert))
Const NumeringSchemeThreshold = 1000
Const TotalResultLength = 3
Const PaddingChar = "0"c
If (toConvert < NumeringSchemeThreshold) Then Return toConvert.ToString().PadLeft(TotalResultLength, PaddingChar)
Dim adjustedValue = (toConvert - NumeringSchemeThreshold) 'since we're starting with this numbering scheme at NumeringSchemeThreshold
Const NumbersInAlphabet = 26
Dim moddedValue = (adjustedValue Mod NumbersInAlphabet) '0 to NumbersInAlphabet - 1 based on the cycle
Dim numCycles = Fix(adjustedValue / NumbersInAlphabet) 'What "cycle number" it is, i.e. the number of times around the alphabet loop.
Dim suffix As String = String.Empty
Dim prefix As String = CStr(numCycles)
Const firstStepThreshold = 100
Const secondStepThreshold = 110
Const LastAlphabetChar = "Z"c
If (numCycles >= firstStepThreshold And numCycles < secondStepThreshold) Then
numCycles = numCycles - firstStepThreshold
prefix = CStr(numCycles)
suffix = LastAlphabetChar
ElseIf (numCycles >= secondStepThreshold) Then
numCycles = numCycles - secondStepThreshold
suffix = LastAlphabetChar & LastAlphabetChar
prefix = String.Empty
End If
Const AsciiCharValueOffset = 65
'concat the cycle number to the converted modded letter, and return the zero-padded result.
Return (prefix & CChar(Char.ConvertFromUtf32(moddedValue + AsciiCharValueOffset)) & suffix).PadLeft(TotalResultLength, PaddingChar)
End Function
So I have a pretty simple UDF written in visual basic for use in excel. It calculates your approx. taxes. Lets say I use it as such:
=Taxes(I23-I18,I24-I20,"Married")
If I type this in it works great. Now if I save the sheet and restart excel the cell now says #VALUE! If I select the formula and hit enter once again it recalculates it fine. What am I doing wrong? Application.Volatile shouldn't be needed but I was trying ideas...
Private Type TaxBracket
Perc As Double
Floor As Currency
Limit As Currency
End Type
Public Function Taxes(gross1 As Currency, gross2 As Currency, filingStatus As String) As Currency
Application.Volatile True
Dim brackets(6) As TaxBracket
Dim stdDeduction As Currency
Dim ssTaxRate As Double
Dim medicareTaxRate As Double
Dim Tax As Double
stdDeduction = 5700
ssTaxRoof = 106800
ssTaxRate = 0.062
medicareTaxRate = 0.0145
Tax = medicareTaxRate * (gross1 + gross2)
Tax = Tax + IIf(gross1 < ssTaxRoof, ssTaxRate * gross1, ssTaxRate * ssTaxRoof)
Tax = Tax + IIf(gross2 < ssTaxRoof, ssTaxRate * gross2, ssTaxRate * ssTaxRoof)
brackets(0).Perc = 0.1
brackets(1).Perc = 0.15
brackets(2).Perc = 0.25
brackets(3).Perc = 0.28
brackets(4).Perc = 0.33
brackets(5).Perc = 0.35
If filingStatus = "Single" Then
brackets(0).Floor = 0
brackets(1).Floor = 8375
brackets(2).Floor = 34000
brackets(3).Floor = 82400
brackets(4).Floor = 171850
brackets(5).Floor = 373650
brackets(0).Limit = 8375
brackets(1).Limit = 34000
brackets(2).Limit = 82400
brackets(3).Limit = 171850
brackets(4).Limit = 373650
brackets(5).Limit = 1000000000
Tax = Tax + incomeTaxes(gross1, brackets, stdDeduction) + incomeTaxes(gross2, brackets, stdDeduction)
ElseIf filingStatus = "Married" Then
brackets(0).Floor = 0
brackets(1).Floor = 16750
brackets(2).Floor = 68000
brackets(3).Floor = 137300
brackets(4).Floor = 209250
brackets(5).Floor = 373650
brackets(0).Limit = 16750
brackets(1).Limit = 68000
brackets(2).Limit = 137300
brackets(3).Limit = 209250
brackets(4).Limit = 373650
brackets(5).Limit = 1000000000
Tax = Tax + incomeTaxes(gross1 + gross2, brackets, stdDeduction * 2)
Else
Taxes = "N/A"
Return
End If
Taxes = Tax
End Function
Private Function incomeTaxes(gross As Currency, brackets() As TaxBracket, deduction As Currency) As Currency
Dim Tax As Double
Dim taxable As Double
Tax = 0
taxable = gross - deduction
For i = 0 To 5
If taxable > brackets(i).Limit Then
Tax = Tax + (WorksheetFunction.Min(taxable, brackets(i).Limit) - brackets(i).Floor) * brackets(i).Perc
Else
If taxable > brackets(i).Floor Then
Tax = Tax + (taxable - brackets(i).Floor) * brackets(i).Perc
Else
'tax = tax
End If
End If
Next i
incomeTaxes = Tax
End Function
Your UDF's look OK, apart from using Currency data types (probably should be using doubles or variants since that is what Excel uses).
The usual reason for getting #Value with a UDF is that one of the input arguments cannot be converted to the correct type. If your input cells do not contain numeric values when the workbook opens you would get #Value. This might be caused by calculation sequence problems resulting in one of the upstream precedent cells being uncalculated the first time the function is called. Try declaring the input parameters as variant rather than currency and add some temporary debug.print statements to show the input parameters in the Immediate window.