+ Reply to Thread
Results 1 to 4 of 4

Adding two values

  1. #1
    Greg
    Guest

    Adding two values

    I have a question about the following code,

    Dim ans

    On Error Resume Next


    ans = Application.Match(CLng(Label1.Caption), Range("A:A"), 0)
    If Not IsError(ans) Then
    Application.Index(Range("B:B"), ans) = Label2.Caption +
    ActiveCell.Value


    Else
    MsgBox "Invalid code"
    End If
    On Error GoTo 0
    What I am trying to achieve is with the Application.Index line
    How do I get the two values to add together I have tested the code and it
    puts the figures where they should go but what i am trying to make happen is
    if the cell already has value in it.

    i.e. the cell has 2 in it already and label2 has the value of 5 i want i to
    show 7 in the cell.

    I believe the following line is the problem

    = Label2.Caption + ActiveCell.Value

    Thanks

    Greg



  2. #2
    Duncan
    Guest

    Re: Adding two values

    Greg,

    Ive had the same problem before trying to sum a caption, the workaround
    I use is to have a textbox on the egde of the form where it cannot be
    seen, send the value to the textbox and sum the textbox then remove the
    data again to the caption.

    long winded but I dont know if you can sum a caption.

    HTH.

    Duncan


  3. #3
    Greg
    Guest

    Re: Adding two values

    Thanks Duncan I guess as much I just wrote a huge code.

    Thanks
    Greg
    "Duncan" <[email protected]> wrote in message
    news:[email protected]...
    > Greg,
    >
    > Ive had the same problem before trying to sum a caption, the workaround
    > I use is to have a textbox on the egde of the form where it cannot be
    > seen, send the value to the textbox and sum the textbox then remove the
    > data again to the caption.
    >
    > long winded but I dont know if you can sum a caption.
    >
    > HTH.
    >
    > Duncan
    >




  4. #4

    Re: Adding two values

    Hi Greg,

    Just pass the value of the label to a variable and then add that to the
    activecell see code below. I think the problem you were having is that
    a caption is a string not a value.

    Dim ans
    Dim lbl2 as interger

    On Error Resume Next


    ans = Application.Match(CLng(Label1.Caption), Range("A:A"), 0)
    If Not IsError(ans) Then
    lbl2 = cint(Label2.Caption)
    Application.Index(Range("B:B"), ans) = lbl2 + ActiveCell.Value


    Else
    MsgBox "Invalid code"
    End If
    On Error GoTo 0

    This is untested code. Cint converts the caption to an interger.

    Any problems then post back.

    James


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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