【发布时间】:2021-09-20 21:04:04
【问题描述】:
我在之前的帖子中询问过一个宏,可以将具有相同文本的所有单元格与单个单元格的颜色匹配。这样我就可以在父单元格中着色,运行宏,它会找到所有具有相同文本的子单元格并为我着色。在你们中的一些人的帮助下,我已经弄清楚了。
快进了大约一个月,我意识到在我的工作簿中我忘记了一个子单元格的条目,并将其添加到工作簿的所有工作表中。现在的问题是宏不会为新的子单元格着色。我的代码可以在下面看到,我特意制作了.Range("C1:K75"),大到足以容纳任何新的子单元格,所以我认为这不是问题.另一个地方的工作簿中已经存在另一个具有相同文本的子单元格,我已经复制粘贴了父单元格和原始子单元格中的文本,以确保文本完全匹配。原来的子单元格会被着色,但是新的子单元格仍然不会。我在谷歌上搜索了关于 vba 代码会计新单元格的信息,但似乎找不到任何东西。没有错误代码弹出或任何东西,它只是不会在该单元格中着色,就好像它甚至不存在一样。任何帮助表示赞赏。
Sub MatchAll()
'This will match the color of documents in the Job Description Reqs. to the color
'of the documents in the documents list, color in document on the list on the left side,
'run the macro, and it should make all the same documents in the sheet the same color
Dim c As Range, r As Range, i As Long
Dim sDocument As String
With ActiveSheet
For i = 8 To 200 'Runs the macro for every cell
Set r = .Range("A" & i) 'from A8:A200
sDocument = CStr(r.Value) 'name of document you are looking for is now equal to name in document list
If Not sDocument = vbNullString Then 'if the cell in the document list is not blank then
With .Range("C1:K75") 'within the range of the Job Description Reqs.
Set c = .Find(sDocument, LookIn:=xlValues) 'look for the name of document stated in document list
If Not c Is Nothing Then 'if a document within the range of Job Description Reqs. is found
If Not c.Interior.Color = r.Interior.Color Then 'and the documents in both the list and Job Description Reqs. are not the same color
Do
c.Interior.Color = r.Interior.Color 'make the color of the Job Description Req. Document = the color of the list document
Set c = .FindNext(c) 'find the next document listed in the Job Description Reqs,
Loop While Not c.Interior.Color = r.Interior.Color 'Repeat until all documents of that name are the same color
End If
End If
End With
End If
Next i 'Move onto next document in document list and repeat
End With
End Sub
【问题讨论】:
-
不确定,但您的问题可能是
Loop While Not c.Interior.Color = r.Interior.Color。如果您找到一个与颜色匹配的单元格,您是否会提前结束循环 - 是否还有其他不匹配但您会错过的单元格?