Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2007 Auto format column conditional on another column 2

Status
Not open for further replies.

BxWill

MIS
Mar 30, 2009
367
US
Have a 3 column 2500 row Excel 2007 worksheet;

Name---Type------Value
Jane---Cost------1234
Jill---Service---1234

Is it possible to auto-format the figures in the column titled "Value" conditional on what is in the second column.

For example, if "Cost' is in the second column, the figure in the column titled "Value" should be formatted as Currency with two decimal places. If "Service" is in the second column, the figure in the column titled "Value" should be formatted as a general number without any decimal places.


did create the following, but decided to post to gather additional insight to determine if I am on the right path.

Is this how you would tackle this?

Any additional insight is appreciated.

Code:
Private Sub Worksheet_Change(ByVal Target As Range) 
If Target.Count > 1 Then Exit Sub 
If Not Intersect(Target, [A1]) Is Nothing Then 
Set RNG = Range("B2:B2500") 
	Select Case Target 
		Case "Cost" 
			RNG.NumberFormat = "#,##0.00;[Red]-[$809]#,##0.00" 
		Case "Service" 
			RNG.NumberFormat = "#,##000 [$€-1];[Red]-#,##0.00 [$€-1]" End Select 
End If 
End Sub


 


hi,

Here's how I would do it...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    If Target.Count > 1 Then Exit Sub
    
    Set rng = Cells.Find("Type").Offset(1)
    
    If Not Intersect(Target, Range(rng, rng.End(xlDown))) Is Nothing Then
        Select Case Target
            Case "Cost"
                Target.Offset(0, 1).NumberFormat = "#,##0.00;[Red]-[$809]#,##0.00"
            Case "Service"
                Target.Offset(0, 1).NumberFormat = "#,##000 [$€-1];[Red]-#,##0.00 [$€-1]"
        End Select
    End If
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here's how I would do it:

Conditional Formatting.

You can format number display on conditional formatting!
 



Gruuuu, U R absolutely correct!!!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Did slightly change the structure of the data.

In column B, from B5 to B246, I have one of the following text;
Cost
Service

In the range from E5 to J246, I have various figures that are not formatted.

I would like to "auto format" all of the figures in the range E5 to J246 based on the text that is in column B.

For example, if column B contain "Cost", then I would like the entire row from column E to column J to format as $10,000.
If column B contain "Service", then I would like the entire row from column E to colum J to format as

For example, if "Cost' is in the second column, the figure in the column E to column J should be formatted as Currency with two decimal places. If "Service" is in the second column, the figures in column E to column J should be formatted as a general number without any decimal places.

How should the following code be modified?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    If Target.Count > 1 Then Exit Sub

    Set rng = Cells.Find("Type").Offset(3)

    If Not Intersect(Target, Range(rng, rng.End(xlDown))) Is Nothing Then
        Select Case Target
            Case "Cost/Service"
                Target.Offset(0, 1).NumberFormat = "#,##0.00;[Red]-[$809]#,##0.00"
            Case "Services"
                Target.Offset(0, 1).NumberFormat = "#,##000 [$€-1];[Red]-#,##0.00 [$€-1]"
        End Select
    End If
End Sub





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top