简体   繁体   English

EXCEL中每个单元格中限制部分的条件格式

[英]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)".假设我们有 3 个单元格,内容分别为“123(45)”、“456(67)”和“789(89)”。 Is it possible to formatting the three cells based on only their first values, ie, "123", "456", "789" ?是否可以仅根据它们的第一个值来格式化三个单元格,即 "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,假设我在每个单元格中的每个数字后面都有“(XX)”,如何仍然按照图片所示进行格式化,

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

if the cell contain the same pattern for example: 123 (45)如果单元格包含相同的模式,例如: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 B1 如果你想引用某个单元格的值

and do the desired formatting并进行所需的格式化

if the pattern is not the same but for example some are like: 123 (45), 1234 (46) then:如果模式不一样,但例如有些像:123 (45), 1234 (46) 那么:

=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!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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