Consulting

Results 1 to 10 of 10

Thread: Forced-In negative value

  1. #1
    VBAX Tutor joelle's Avatar
    Joined
    Apr 2005
    Location
    Sunnyvale, CA
    Posts
    204
    Location

    Forced-In negative value

    Hello Experts,

    Please I need your expert help with the following:

    Cell A1 is an input field for credit, meaning it has to be a negative value
    all the times for the total to calculate correctly. How do I do so that even if
    people enter a positive or a negative value, it will still turn to a negative value
    and a negative number display. I run into a circular problem here because I
    cannot put formula into A1 since I make it an input field.

    Your prompt help is very much appreciated.
    Regards,
    Nee

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Something like this maybe? (code goes into the relevant sheets code module)

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
          If IsNumeric([a1]) Then [a1] = -Abs([a1])
    End Sub
    HTH,

    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Tutor joelle's Avatar
    Joined
    Apr 2005
    Location
    Sunnyvale, CA
    Posts
    204
    Location
    Hello John,

    Wow that's very quick - thanks so much. But is there a way to avoid using
    vba? Or if I need to use the code you provided, how do I change them to
    work with a range of A1:A2 and not just A1.

    Many thanks,
    Nee

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Well if you can't use a formula, then in the VBE, select the code pane for the worksheet concerned and add some code to do it in the WorkSheet_Change event

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$1" And Target.Value > 0 Then
            Target.Value = 0 - Target.Value
        End If
    End Sub
    Of course if someone enters a non-numeric value, 0 - Target.Value will cause an error, so you need to validate the input
    K :-)

  5. #5
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Quote Originally Posted by Nee
    Hello John,

    Wow that's very quick - thanks so much. But is there a way to avoid using
    vba? Or if I need to use the code you provided, how do I change them to
    work with a range of A1:A2 and not just A1.

    Many thanks,
    Nee
    Too quick, and a better answer!!!

    But since we mentioned validation, you can go to Data>Validation, select Custom and enter =A1<1 for the formula, and add a custom message for the user
    K :-)

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Nee,

    To add another range for it to work with, do it this way

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If IsNumeric([a1]) Then [a1] = -Abs([a1])
    If IsNumeric([a2]) Then [a2] = -Abs([a2])
    Application.EnableEvents = True
    End Sub
    From what you said before, I can't see that you can do it as a worksheet solution, but if you did you could use something like =-ABS(G11) ... assuming here that G11 is where you're getting your value for A1 from.

    @ Killian we musta posted at the same time
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    To make it display negative no matter what use this.

    Format | Cells | Number | Custom
     
    -#,##0_);-#,##0
    This will make the number "look" negative even if it is positive, but you will need to adjust your formulas using this cell as well.

    Option 1:

    Have a hidden column with the formula:

    =-Abs(A1)
    Then refer to the cells in that column instead of column A.

    Option 2:

    In all formulas that refer to A1 (for example), change them from A1 to -Abs(A1).

  8. #8
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Quote Originally Posted by johnske
    @ Killian we musta posted at the same time
    and now you've posted while I was editing my response

    That's it! I'm going to bed...
    K :-)

  9. #9
    VBAX Tutor joelle's Avatar
    Joined
    Apr 2005
    Location
    Sunnyvale, CA
    Posts
    204
    Location
    Hello Gentlemen!

    No fighting there -- you all deserve a big hug - wow, thats too much
    to sacrify, so how about a pat on the shoulder! Seriously, you all have done great!

    My thanks to you too, Killian and Jake.
    John, your codes work great - and so do Killian's.
    Jake - I like your alternative too because my coworkers are afraid of seeing vba (am I too?)

    Good nite everyone,

    Nee

  10. #10
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by Killian
    Too quick, and a better answer!!!

    But since we mentioned validation, you can go to Data>Validation, select Custom and enter =A1<1 for the formula, and add a custom message for the user
    be very careful about using Validation. Validation is a "format" and is easily wiped out if a copy/paste is executed into the cell with Validation.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

Posting Permissions

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