Results 1 to 4 of 4

Thread: Insert formulas in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    3

    Insert formulas in VB

    I am fairly new to VB

    I want to enter a formula in VB but don't know how to proceed.

    The formula I use in Excel is as follows

    =IF(B2="A",M2*20.27)+IF(B2="B",M2*15.96,0)

    How can I write a formula to make the same thing work in VB

    Any help would be greatly appreciated.

    TX

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Insert formulas in VB

    you use if,then,else,end if blocks:

    vb Code:
    1. if something = something then
    2.     doSomething
    3. else
    4.     doSomethingElse
    5. end if

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    3

    Re: Insert formulas in VB

    I tried the following

    Private Sub UserForm_Click()
    If txtFinish - txtStart >= 6.1 Then
    txtLunch = 0.5
    Else
    txtLunch = 0
    End If


    End Sub

    This is a string that automatically adds lunch if time from Start to Finish exceeds 6 hours.

    Any ideas???

    TX

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Insert formulas in VB

    it looks like you're trying to perform arithmetic on strings. try using timespans:

    vb Code:
    1. Dim startTime As String = "05:45:00"
    2. Dim ts1 As TimeSpan = TimeSpan.Parse(startTime)
    3. Dim endTime As String = "12:00:00"
    4. Dim ts2 As TimeSpan = TimeSpan.Parse(endTime)
    5. MsgBox((ts2 - ts1).ToString)

Posting Permissions

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



Click Here to Expand Forum to Full Width