Consulting

Results 1 to 9 of 9

Thread: Label Calculation

  1. #1

    Label Calculation

    I have 2 userform labels (TDay & LDay). Can someone assist in a code that will subtract LDay from TDay and display the results in RDay, which is also a userform label. Thank you for your assistance

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Convert the captions to numbers
    [VBA]Label3.Caption = CLng(Label1.Caption) + CLng(Label2.Caption)[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    I forgot to mention, the labels caption display time format. Can you assist. thanks

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]Label3.Caption = Format(TimeValue(Label1.Caption) - TimeValue(Label2.Caption), "hh:mm")
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    For some reason, i'm getting a type mismatch.

    here is what I have. the various cells have numbers that are time formated. When the userform is displayed, I had to display the time that is in cell as a text so that it can display in a label as (i.e. 32hr & 52min (32:52)).

    [vba] LDay = Range("AF2").Text
    TDay = Range("AF3").Text
    RDay.Caption = Format(TimeValue(TDay.Caption) - TimeValue(LDay.Caption), "hh:mm")
    [/vba]

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a workbook with a simple userform showing your label data?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    See Attached
    Attached Files Attached Files

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This can probably be simplified, but TimeValue won't work with such numbers

    [VBA]Private Sub CommandButton1_Click()

    x = Label1.Caption
    t1 = Split(x, ":")(0) / 24 + Split(x, ":")(1) / 24 / 60
    y = Label2.Caption
    t2 = Split(y, ":")(0) / 24 + Split(y, ":")(1) / 24 / 60

    t = 24 * (t2 - t1)
    Label3.Caption = Int(t) & ":" & Format((t - Int(t)) * 60, "00")
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    ok...I'll give it a try

Posting Permissions

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