[英]Excel VBA to find text and apply font color
MY EXCEL FILE我的 Excel 文件
Two worksheets: Report and Leaving两个工作表:报告和离开
One Named Range: Leavers (list of full names in the Leaving worksheet, column A , some names are red and others are orange ).一个命名范围: Leavings ( Leaving工作表中的全名列表, A 列,一些名字是红色的,另一些是橙色的)。
MY GOAL我的目标
I want my macro to find in the Report worksheet (specifically in columns G to M) all the names that are part of the Leavers named range and apply the matching font color to each cell that was found.我希望我的宏在报告工作表中(特别是在 G 到 M 列中)找到属于Leavers命名范围的所有名称,并将匹配的字体颜色应用于找到的每个单元格。
MY CODE (SO FAR...)我的代码(到目前为止...)
This code could help by searching them one by one but it doesn't change much from doing it manually with Ctrl + F one by one.此代码可以通过一一搜索来提供帮助,但与使用 Ctrl + F 手动操作并没有太大变化。 I could not find another way around it.我找不到其他方法。 Feel free to offer better alternatives, codes and solutions.随意提供更好的替代方案、代码和解决方案。
Dim Sh As Worksheet
Dim Found As Range
Dim Nme As String
Dim Adr1 As String
Nme = Application.InputBox("Enter Name to search", "Test")
Set Sh = Sheets("Sheet1")
With Sh.Range("A2:A")
Set Found = .Find(What:=Nme, After:=.Range("A2"), _
LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Found Is Nothing Then
Adr1 = Found.Address
Else
MsgBox "Name could not be found"
Exit Sub
End If
Do
Found.Interior.ColorIndex = 4
Set Found = .FindNext(Found)
Loop Until Found Is Nothing Or Found.Address = Adr1
End With
End Sub
Try this:尝试这个:
I tried to stick with some of your existing code but I had to make some changes.我试图坚持使用您现有的一些代码,但我不得不进行一些更改。
You need to loop through your first range (I've used "G2:M1000" here on Sheet1
which I guess is your report
page?)您需要遍历您的第一个范围(我在Sheet1
上使用了“G2:M1000”,我猜这是您的report
页面?)
You can't use a range like "A2:A"
in your find routine, so again I've arbitrarily used a 1000 limit: "A2:A1000"
您不能在查找例程中使用像"A2:A"
这样的范围,所以我再次任意使用了 1000 限制: "A2:A1000"
You were using interior cell colour, not font colour, I've changed this but if you did mean interior colour then just swap it back您使用的是内部单元格颜色,而不是字体颜色,我已更改此设置,但如果您确实指的是内部颜色,则只需将其换回
I'm not using "Exit Sub"
since this will stop everything running the first time it encounters a blank cell / no matching name.我没有使用"Exit Sub"
,因为这将在第一次遇到空白单元格/没有匹配名称时停止所有运行。
Sub eh()
Dim rng As Range: Set rng = Sheet1.Range("G2:M1000")
Dim v As Range
Dim c As Variant
Dim Found As Range
Dim Nme As String
Dim Adr1 As String
For Each v In rng
Nme = v.Value2
If Nme <> "" Then
Set Found = Sheet2.Range("A2:A1000").Find(What:=Nme, After:=Sheet2.Range("A2"), LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not Found Is Nothing Then
v.Font.Color = Found.Font.Color
End If
End If
Next v
End Sub
I tried a different approach, I have a subprocedure to loop through the range of the leavers, and when I find a value i give it to another procedure to look for that value in the report range.我尝试了一种不同的方法,我有一个子过程来遍历离开者的范围,当我找到一个值时,我将它交给另一个过程以在报告范围中查找该值。 I am copying all the format of the cells, you can change it to just copy the font color.我正在复制单元格的所有格式,您可以将其更改为仅复制字体颜色。 Also you should check for the ends of each range, to optimize and also this code would loop several times for the same name if the same name repeats in the leavers range, that could be improved.此外,您应该检查每个范围的末端,以进行优化,并且如果在离开者范围内重复相同的名称,则此代码将循环多次以获得相同的名称,这可以改进。
Sub select_name() 'Select every cell in the leavers range
Dim leaving_range As Range
Dim row_leaving As Range
Set leaving_range = Sheets("Leaving").Range("A2:A10") 'The leavers list, check for the end of the range
For Each row_leaving In leaving_range.Rows
If row_leaving.Cells(1, 1).Text <> "" Then
row_leaving.Cells(1, 1).Copy 'I am gonna copy all the format, you change it to copy just the font color
Call look_for_name(row_leaving.Cells(1, 1).Text)
End If
Next
End Sub
Sub look_for_name(name_to_find As String) 'look for a name in the report range and change the format
Dim report_range As Range
Dim row_report As Range
Set report_range = Sheets("Report").Range("G1:M5") 'the report range where the names are to be found
For Each row_report In report_range.Rows
For Each cell In row_report.Cells
If cell.Value = name_to_find Then
cell.PasteSpecial Paste:=xlPasteFormats
End If
Next
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.