简体   繁体   中英

Conditional formatting for restricted part in each cell in EXCEL

Suppose we have 3 cells, with content respectively "123 (45)", "456 (67)" and "789 (89)". Is it possible to formatting the three cells based on only their first values, ie, "123", "456", "789" ?

To make it clear:

在此处输入图片说明

How to still format as shown in the picture suppose I have "(XX)" following each number in each cell,

ie, 480 (XX), 7 (XX), 112 (XX)''''''''

if the cell contain the same pattern for example: 123 (45)

then you can use Conditional Formatting the formula-based option and put this formula:

=NUMBERVALUE(LEFT(A1,3)) = 123 

or

=NUMBERVALUE(LEFT(A1,3))=B1

B1 if you want to reference to the value of some cell

and do the desired formatting

if the pattern is not the same but for example some are like: 123 (45), 1234 (46) then:

=NUMBERVALUE(LEFT(A1,FIND(" ",A1)-1)) = 123

or

=NUMBERVALUE(LEFT(A1,FIND(" ",A1)-1)) =B1

EDIT

The easy way of doing that is:

In another column next to the column which has tha data put this formula:

=NUMBERVALUE(LEFT(A1,FIND(" ",A1)-1))

then you can apply data bars conditional formatting, inside the conditional formatting options for data bar there is a check box "show bar only" and the result is going to be something like this:

在此处输入图片说明

Another way is in another column put the formula:

=A1&"     "&REPT("|",NUMBERVALUE(LEFT(A1,FIND(" ",A1)-1))/5)

在此处输入图片说明

or this

=A8&"     "&REPT("∙",NUMBERVALUE(LEFT(A8,FIND(" ",A8)-1))/5)

在此处输入图片说明

there is no other easy way of doing that cause data bar conditional formatting does not accept array formula.

You can do it anyway with a macro in the sheet which has the data copy and paste the following code:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim lRow As Long

Dim data As Range

Set data = ActiveSheet.Range("A:A")

If Not Intersect(Target, data) Is Nothing Then

lRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

Dim arr() As Double

On Error Resume Next
For i = 1 To lRow

Set myrange = ActiveSheet.Range("A" & i)

ReDim Preserve arr(i)
arr(i) = Left(myrange, WorksheetFunction.Find(" ", myrange) - 1)


Next i

Dim MaxValue As Long

MaxValue = WorksheetFunction.Max(arr)

For i = 1 To lRow
Set myrange = ActiveSheet.Range("A" & i)

With myrange.Interior
  .Pattern = xlPatternLinearGradient
  .Gradient.Degree = 180
  .Gradient.ColorStops.Clear
End With
With myrange.Interior.Gradient.ColorStops.Add(1)
  .Color = RGB(13, 71, 161)
  .TintAndShade = 0
End With
With myrange.Interior.Gradient.ColorStops.Add(1 - (arr(i) / MaxValue))
  .Color = RGB(13, 71, 161)
  .TintAndShade = 1
End With

With myrange.Interior.Gradient.ColorStops.Add(0)
  .Color = RGB(255, 255, 255)
  .TintAndShade = 1
End With

Next i
End If
End Sub

So with this code every time you put a new data in the column it will automatically format it like the image below. I have tested and it works just paste it in your sheet, not in a module and change the ranges as fits to you.

在此处输入图片说明

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM