通过更改数字颜色标记号码,如果find

我在编写代码时遇到问题,我将开始解释:

1-代码将看这张表并采取本地号码sheet1

2-如果数字与另一张纸上的数字相匹配,颜色将变为红色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 

你没有设置最后一个,所以你不要在循环中inputFor x = 2 to finalrow 。 因此没有任何反应 把finalrow = Range("A1000000").End(xlUp).Row在循环外面,因此它进入它。

你可以使用这个而不是四个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