+ Reply to Thread
Results 1 to 12 of 12

Textbox negative values not showing correctly

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,474

    Textbox negative values not showing correctly

    The ROI($) value is the SALE - INVESTMENT.
    The ROI(%) value is the (SALE-INVESTMENT)/SALES

    If the values are negative they are red and in brackets, but both still show the Minus sign, and the ROI (%) has a leading "Hashmark".

    Option Explicit
    Dim a As Long, f As Long
    Dim c As Range
    Dim ws As Worksheet
    
    Private Sub CommandButton2_Click()
    
        If tboxSPINV <> "" And tboxSPSALES <> "" Then
        
        tboxSPROICASH.Value = tboxSPSALES - tboxSPINV
            If tboxSPROICASH.Value < 0 Then
            tboxSPROICASH.Value = Format(tboxSPROICASH, "($#,##0.00)")
            tboxSPROICASH.ForeColor = RGB(255, 0, 0)
            
            Else: tboxSPROICASH.Value = Format(tboxSPROICASH, "$#,##0.00)")
            tboxSPROICASH.ForeColor = RGB(0, 0, 0)
            End If
            
        tboxSPROIPER.Value = (tboxSPSALES - tboxSPINV) / tboxSPSALES
            If tboxSPROIPER < 0 Then
            tboxSPROIPER.Value = Format(tboxSPROIPER.Value, "(\#,##0.00%)")
            tboxSPROIPER.ForeColor = RGB(255, 0, 0)
            
            Else: tboxSPROIPER.Value = Format(tboxSPROIPER.Value, "#,##0.00%")
            tboxSPROIPER.ForeColor = RGB(0, 0, 0)
            End If
            
        End If
       
    End Sub
    
    Private Sub CommandButton4_Click()
    
        Unload Me
    
    End Sub
    Any solutions, suggestions or alternatives received gratefully as ever

    Ochimus
    Attached Files Attached Files
    Last edited by Ochimus; 05-03-2019 at 10:40 AM.

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,787

    Re: Textbox negative values not showing correctly

    And the password is?

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Textbox negative values not showing correctly

      If tboxSPROICASH.Value < 0 Then
        tboxSPROICASH.Value = Format(tboxSPROICASH, "$#,##0.00;($#,##0.00)")
      
      ' ...
      
      If tboxSPROIPER < 0 Then
        tboxSPROIPER.Value = Format(tboxSPROIPER.Value, "#,##0.00%;(#,##0.00%)")
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Textbox negative values not showing correctly

    You can specify a format for positive and negative:

    "#,##0.00%;(#,##0.00%)"

    the \ in your format string was the reason for you getting a hash mark.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Textbox negative values not showing correctly

    Or ...

    Private Sub CommandButton2_Click()
      Dim d             As Double
    
      If tboxSPINV <> "" And tboxSPSALES <> "" Then
        d = tboxSPSALES.Value - tboxSPINV.Value
        With tboxSPROICASH
          .Value = Format(d, "$#,##0.00;($#,##0.00)")
          If d >= 0 Then .ForeColor = vbBlack Else .ForeColor = vbRed
        End With
    
        d = 1# - tboxSPINV.Value / tboxSPSALES.Value
        With tboxSPROIPER
          .Value = Format(d, "#,##0.00%;(#,##0.00%)")
          If d >= 0 Then .ForeColor = vbBlack Else .ForeColor = vbRed
        End With
      End If
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,474

    Re: Textbox negative values not showing correctly

    Fluff13,

    Genuinely stumped on this, because the attachment was "unlocked", so no idea how it had a password when you opened it?

    Can you try and open it again?

    Ochimus

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,787

    Re: Textbox negative values not showing correctly

    The VBA project is definitely password protected.
    Although that may now be irrelevant, as you got some solutions.
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,474

    Re: Textbox negative values not showing correctly

    shg,

    Appreciate the prompt response, but ".forecolor" throws a Compile error "invalid or unqualified reference"?

    Having remembered that "RGB" returns a Long, so tried declaring "forecolor" as Long, but that generated the message that "Member already exists in an Object Module from which this Object Module derives"

    Never come across before?

    Ochimus

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Textbox negative values not showing correctly

    Compiles fine here. You sure you copied the code correctly?

  10. #10
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,474

    Re: Textbox negative values not showing correctly

    Fluff,

    Apologies, but when I attached the file at 09:37 I must have used the "Protected" version . When I amended the entry three minutes later, I attached the "unlocked" version, which the others obviously found when they clicked on to the question.

    Ochimus

  11. #11
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,474

    Re: Textbox negative values not showing correctly

    Gentlemen,

    Turning into "One step forward, Two back".

    Tried the approaches on the "Stage 2" document which has an extra element.
    Losses appear correctly in brackets with red font, but ROI is not working, as explained on the front sheet of the attached.
    Can't see where the logic is falling over, but confident someone will.

    Private Sub CommandButton2_Click()
    
    'Calculate Spring profit
    
        If tboxSPBUY <> "" And tboxSPSALES <> "" Then
        
        d = tboxSPSALES.Value - tboxSPBUY.Value - tboxSPINV
        tboxSPPROFIT.Value = d
        e = d / (tboxSPBUY.Value + tboxSPINV.Value)
        tboxSPROI.Value = e
            
        With tboxSPPROFIT
        .Value = Format(tboxSPPROFIT, "$#,##0.00;($#,##0.00)")
            If d >= 0 Then .forecolor = vbBlack Else .forecolor = vbRed
            End With
        
        With tboxSPROI
        .Value = Format(e, "#,##0.00%;(#,##0.00%)")
            If e >= 0 Then .forecolor = vbBlack Else .forecolor = vbRed
            End With
      
        End If
    
    End Sub
    Ochimus
    Attached Files Attached Files

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: Textbox negative values not showing correctly

    You need to be very careful when adding strings. Use val to convert the textboxes to actual number values:

        e = d / (Val(tboxSPBUY.Value) + Val(tboxSPINV.Value))

+ 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. Replies: 2
    Last Post: 12-19-2016, 05:34 PM
  2. showing Labels and Textbox values
    By zaku in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-17-2015, 12:30 PM
  3. [SOLVED] Negative or zero values cannot be plotted correctly on log charts
    By NTE in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 03-28-2012, 11:32 AM
  4. Replies: 2
    Last Post: 06-19-2011, 11:48 AM
  5. Excel 2007 : Values not showing correctly in chart
    By kuroneko4 in forum Excel General
    Replies: 1
    Last Post: 01-21-2010, 12:54 PM
  6. Replies: 1
    Last Post: 08-18-2009, 07:35 AM
  7. Showing Negative Values As 0
    By AlanWade in forum Excel General
    Replies: 6
    Last Post: 06-25-2008, 02:52 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