+ Reply to Thread
Results 1 to 6 of 6

VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

  1. #1
    Registered User
    Join Date
    03-25-2014
    Location
    Midwest USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Question VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    I am developing a simple general ledger for employees to use for expense tracking. I want the contents of each cell in Column E to change to a negative value based on the content of the adjacent cell in Column F. Column F is restricted to a drop down list. When the employee chooses "Journal Entry - Credit" the value in Column E should change to a negative. I think VBA is required for this, but have been unable to get it to work.
    I'm using the code below, adopted from a post I found on the forum, but not getting the negative value.

    Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Application.EnableEvents = False

    For Each cell In Target
    If Not Intersect(cell, Range("F:F")) Is Nothing Then
    Select Case Trim(LCase(cell.Value))
    Case "Journal Entry - Credit"
    Cells(Target.Row, "E") = Abs(Cells(Target.Row, "E")) * -1
    Case Else
    Cells(Target.Row, "E") = Abs(Cells(Target.Row, "E"))
    End Select
    End If
    Next cell

    Application.EnableEvents = True
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    Maybe:

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    Count Manager,
    Welcome to the forum!
    Please take a moment to read forum's rule.
    Please use code tags with your code.
    The ABS function will convert any negative in to +ve

    Please Login or Register  to view this content.
    Will revert to +ve

  4. #4
    Registered User
    Join Date
    03-25-2014
    Location
    Midwest USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    Thanks for your offered assistance.
    I tried the revised code and got the same result, though the VBA does appear to make the formatting of the value in "E" change from 'Accounting' to 'Currency' so the dollar sign ($) in the cell moves over. Otherwise I see no change either in the cell or on any of the other worksheets that pull data from this one.
    I'm absolutely clueless with VBA and thought I could simply modify the code for another solution I found on the forum by changing column refefrences and the reference criteria, but it is not working. Perhaps the code is incorrect for my intentions? Any other ideas?

  5. #5
    Registered User
    Join Date
    03-25-2014
    Location
    Midwest USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    AB33 - Thanks, I'll read up on the rules.
    I think your advice is to remove the ABS function as it is negating my effort to create a negative value. Is that correct?

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    Yes,
    You normally use ABS to convert negative in to positive. For e.g. you have 20 and -20. You say ABS(-20)=20

  7. #7
    Registered User
    Join Date
    03-25-2014
    Location
    Midwest USA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA Code to Make a Cell Value Negative Based on Content of Adjacent Cell

    I've tried numerous iterations of this and have determined it is outside my very limited understanding of VBA to get this to work correctly. Can anyone help with a better way? My ABS idea is not working.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Inserting Values in a Cell Depending on Content in an Adjacent Cell
    By lowlybroker in forum Excel General
    Replies: 3
    Last Post: 03-20-2014, 01:23 PM
  2. copying cell content into a specific workbook based on the content in that cell
    By krishna reddy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-20-2012, 03:52 AM
  3. Sumproduct based on adjacent cell ID Code
    By avidcat in forum Excel General
    Replies: 6
    Last Post: 05-03-2009, 02:38 PM
  4. Counting cells with a criteria based on content and adjacent cell
    By ziggy12345 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-03-2008, 03:32 PM
  5. Formatting Code Based on Cell Content
    By Hulk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2005, 12:35 PM

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