如果找到,则通过更改数字颜色来标记数字

时间:2017-08-23 08:06:36

标签: excel vba excel-vba

我的代码工作有问题我将开始解释:

1-代码将查看此工作表并获取机密编号sheet1

2-如果数字与另一张表中的数字匹配,它将改变颜色,该值的颜色将变为红色sheet 2

3 - 代码应该按行进行,所以如果数字存在于其他行中,它将不会改变它们的颜色(只有它自己会改变的行)。

Private Sub CommandButton1_Click()

Dim ContainWord1 As String
Dim finalrow As Integer
Dim x As Integer

   Active.Sheet11
Active.Sheet14

finalrow = Range("A1000000").End(xlUp).Row

For x = 2 To finalrow



ContainWord1 = Sheet15.Cells(x, 10).Value

  If Sheet11.Cells(x + 3, 8).Value = ContainWord1 Then Sheet11.Cells(x + 3, 8).Font.Color = vbRed
   If Sheet11.Cells(x + 3, 10).Value = ContainWord1 Then Sheet11.Cells(x + 3, 10).Font.Color = vbRed
    If Sheet11.Cells(x + 3, 12).Value = ContainWord1 Then Sheet11.Cells(x + 3, 12).Font.Color = vbRed
     If Sheet11.Cells(x + 3, 14).Value = ContainWord1 Then Sheet11.Cells(x + 3, 14).Font.Color = vbRed



  Next x


End Sub

当我使用代码时没有发生任何事情

谢谢你:)

尝试一下:我将最后一个更改为循环并删除字符

现在有效的代码:

Private Sub CommandButton1_Click()

Dim ContainWord1 As String
Dim finalrow As Integer
Dim x As Integer

Range("H5:M100").Font.Color = vbBlack

finalrow = Range("A1000000").End(xlUp).Row

For x = 2 To finalrow

ContainWord1 = Cells(x + 3, 25).Value

  If Sheet11.Cells(x + 3, 8).Value2 = ContainWord1 Then Sheet11.Cells(x + 3, 8).Font.Color = vbRed
   If Sheet11.Cells(x + 3, 10).Value2 = ContainWord1 Then Sheet11.Cells(x + 3, 10).Font.Color = vbRed
    If Sheet11.Cells(x + 3, 12).Value2 = ContainWord1 Then Sheet11.Cells(x + 3, 12).Font.Color = vbRed
     If Sheet11.Cells(x + 3, 14).Value2 = ContainWord1 Then Sheet11.Cells(x + 3, 14).Font.Color = vbRed



  Next x


End Sub

2 个答案:

答案 0 :(得分:2)

您没有设置终结,因此您不会进入循环For x = 2 to finalrow。没有任何反应。将finalrow = Range("A1000000").End(xlUp).Row放在循环之外,然后进入循环。

答案 1 :(得分:0)

您可以使用此代替四个If ...语句

可以更轻松地添加/更改列并更改起始行

Dim cel As Range
For Each cel In Sheet11.Range("h3,j3,l3,n3").Offset(x)
    If cel.Value = ContainWord1 Then cel.Font.Color = vbRed
Next cel