+ Reply to Thread
Results 1 to 3 of 3

Inverting negative values in graph

Hybrid View

  1. #1
    Registered User
    Join Date
    06-15-2017
    Location
    NYC
    MS-Off Ver
    2010
    Posts
    9

    Inverting negative values in graph

    Hey I have the following VBA which graphs my positive values as green and negative as red. I need to put the negative values back above the axis, any idea how to accomplish that?
    Sub FormatPointByCategoryAndValue()
      Dim rColor As Range
      Dim vColor As Variant
      Dim srsColor As Series
      Dim srsColor2 As Series
      Dim iRow As Long
      Dim iCol As Long
      Dim iPoint As Long
      Dim vCategories As Variant
      Dim vValues As Variant
      
    
    
      Const sColorSheetName As String = "ColorSheet"
      Const sColorRangeName As String = "ColorRange"
      
        PosCol = RGB(0, 0, 255) ' colour for positive values
        NegCol = RGB(255, 0, 0) ' colour for negative values
    
        Set srsColor = ActiveChart.SeriesCollection(1)
        
        ' cycle through points
        For iPoint = 1 To Sheet2.Range("Start_Series").End(xlDown).Row - Sheet2.Range("Start_Series").Row + 1
       
            'Extract the column number iPoint
             Set srsColor2 = ActiveChart.SeriesCollection(iPoint)
             With srsColor2
                vValues = .Values
            End With
            For iCol = 1 To srsColor.Points.Count
                If vValues(iCol) < 0 Then
                    ActiveChart.SeriesCollection(iPoint).Points(iCol).Select
                   Selection.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
                   
                ElseIf vValues(iCol) > 0 Then
                    ActiveChart.SeriesCollection(iPoint).Points(iCol).Select
                    Selection.Format.Fill.ForeColor.RGB = RGB(0, 176, 80)
                
                Else
                    ActiveChart.SeriesCollection(iPoint).Points(iCol).Select
                    Selection.Format.Fill.ForeColor.RGB = RGB(255, 255, 255)
                End If
            Next iCol
          Next iPoint
    
    ExitHere:
    End Sub
    Also attached the graph as a picture so you can see what I'm talking about
    Attached Images Attached Images

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Inverting negative values in graph

    Hi
    if by putting negative values above the axis you mean displaying them as if they were positives but in a different colour, I don't think this can be done using points in a single series. There are two ways you could do it though:
    1. create a couple of new series, one with positive values and the other with the negative values multiplied by -1
    2. break the link between the series and its source data on the spreadsheet, then convert negative values in the source data to positive ones (this will not update when you change the source data in the worksheets)

  3. #3
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Inverting negative values in graph

    this macro will use method 2

    Sub ChartsPosNeg()
    Dim PosCol, NegCol, MyCh As ChartObject, MySer As Series
    PosCol = RGB(0, 255, 0) ' colour for positive values
    NegCol = RGB(255, 0, 0) ' colour for negative values
    
    For Each MyCh In ActiveSheet.ChartObjects
        For Each MySer In MyCh.Chart.SeriesCollection
            MySer.Values = MySer.Values
            MySer.Format.Fill.ForeColor.RGB = PosCol
            For x = 1 To MySer.Points.Count
                If MySer.Values(x) < 0 Then MySer.Points(x).Format.Fill.ForeColor.RGB = NegCol
            Next x
            MySer.Formula = Replace(MySer.Formula, "-", "")
            
        Next MySer
    Next MyCh
    End Sub

+ 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] Boolean values inverting when passing to a macro in a separate WB
    By Montador in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-02-2017, 02:48 PM
  2. [SOLVED] Graph with Negative and Positive Values
    By gill123 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-21-2015, 09:46 AM
  3. Do not graph negative numbers
    By ScottL in forum Excel General
    Replies: 2
    Last Post: 12-13-2011, 02:12 PM
  4. Negative Correlation and Scatter Graph
    By mubashir aziz in forum Excel General
    Replies: 1
    Last Post: 02-25-2011, 12:27 AM
  5. Bar Graph with negative values
    By HammerTime in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 05-30-2010, 11:05 PM
  6. Inverting worksheet (not graph) axises
    By RiotLoadTime in forum Excel General
    Replies: 5
    Last Post: 08-07-2006, 11:35 AM
  7. inverting data for a graph
    By Chris in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-29-2006, 12:45 AM

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