+ Reply to Thread
Results 1 to 2 of 2

Change decimal format of cells depending on conditions?

  1. #1
    Cornelius
    Guest

    Change decimal format of cells depending on conditions?


    Hi all. I'm looking to change the format of cells to two-decimals when they
    represent price per pound, but switch to comma-format when otherwise. This
    is because the sheet shows lbs, dollars or price depending on the switch
    selected, or combinations of them in different areas, and the lbs and dollars
    columes are in the millions (decimals are confusing to the audience in this
    case).

    Thanks
    ccc

  2. #2
    Ron Rosenfeld
    Guest

    Re: Change decimal format of cells depending on conditions?

    On Thu, 24 Feb 2005 15:23:08 -0800, Cornelius
    <[email protected]> wrote:

    >
    >Hi all. I'm looking to change the format of cells to two-decimals when they
    >represent price per pound, but switch to comma-format when otherwise. This
    >is because the sheet shows lbs, dollars or price depending on the switch
    >selected, or combinations of them in different areas, and the lbs and dollars
    >columes are in the millions (decimals are confusing to the audience in this
    >case).
    >
    >Thanks
    >ccc


    You can do that by using a VBA event triggered macro (Worksheet_Change, for
    example).

    Here is an example assuming that your values are in A1:A10 and your "switch" is
    in the adjacent column B1:B10. You will obviously have to modify this to suit
    your needs.

    =========================
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim v As Range, switch As Range

    Set v = Range("A1:A10")
    Set switch = Range("B1:B10")

    If Not Intersect(Target, v) Is Nothing Then
    Select Case Target.Offset(0, 1).Value
    Case Is = "$/lb"
    Target.NumberFormat = "#.00"
    Case Is = "$", "lb"
    Target.NumberFormat = "#,#"
    Case Else
    Target.NumberFormat = "General"
    End Select
    End If

    If Not Intersect(Target, switch) Is Nothing Then
    Select Case Target.Value
    Case Is = "$/lb"
    Target.Offset(0, -1).NumberFormat = "#.00"
    Case Is = "$", "lb"
    Target.Offset(0, -1).NumberFormat = "#,#"
    Case Else
    Target.Offset(0, -1).NumberFormat = "General"
    End Select
    End If


    End Sub
    =====================


    --ron

+ 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