BritsBlitz
New Member
- Joined
- Jan 10, 2014
- Messages
- 34
- Office Version
- 365
- Platform
- Windows
I have two worksheets. Sheet 1 contains a list of names from row 1 to 100. Sheet 2 contains a list of the same names with specific conditions from row 1 to 250. The names on Sheet 1 need to change to a specific color based on the conditions associated with that same name on Sheet2.
I have a vba code that will loop through the names on Sheet 1, compare it to the list on Sheet 2 to find its match, then look at the condition on Sheet 2 and change the text color on Sheet1 if the condition is met. Below is an example of the two sheets
Sheet1 (100 rows):
Sheet2 (250 rows):
If my condition specifies “Yes”, then John Smith’s & Steve Smith’s names on Sheet 1 should be highlighted in BLUE.
Below is the code I’m using. This code WORKS. What I’m asking is if there is a way to optimize the code to make it run faster. Since my code basically compares each of the 100 lines on Sheet1 x250 times to find all the matches, it takes a long time to run. Any suggestions on how to optimize this code so it doesn’t do 25,000 comparisons each time?
***********************************************
Private Sub Names()
Dim a As Integer
Dim b As Integer
For a = 1 To 100
For b = 1 To 250
If Worksheets("Sheet1").Cells(a, 1).Value = Worksheets("Sheet2").Cells(b, 1).Value And (Worksheets("Sheet2").Cells(b, 2).Value = "Yes" Or Worksheets("Sheet2").Cells(b, 3).Value = "Yes") Then
Worksheets("Sheet1").Cells(a, 1).Font.Color = RGB(0, 176, 240)
Worksheets("Sheet1").Cells(a, 1).Font.Bold = True
End If
Next b
Next a
End Sub
***********************************************
I have a vba code that will loop through the names on Sheet 1, compare it to the list on Sheet 2 to find its match, then look at the condition on Sheet 2 and change the text color on Sheet1 if the condition is met. Below is an example of the two sheets
Sheet1 (100 rows):
John Smith |
Jane Doe |
Steve Smith |
Sheet2 (250 rows):
Mark Jones | No | No |
John Smith | No | Yes |
Jane Doe | No | No |
Steve Smith | Yes | Yes |
Dave Muster | No | No |
If my condition specifies “Yes”, then John Smith’s & Steve Smith’s names on Sheet 1 should be highlighted in BLUE.
Below is the code I’m using. This code WORKS. What I’m asking is if there is a way to optimize the code to make it run faster. Since my code basically compares each of the 100 lines on Sheet1 x250 times to find all the matches, it takes a long time to run. Any suggestions on how to optimize this code so it doesn’t do 25,000 comparisons each time?
***********************************************
Private Sub Names()
Dim a As Integer
Dim b As Integer
For a = 1 To 100
For b = 1 To 250
If Worksheets("Sheet1").Cells(a, 1).Value = Worksheets("Sheet2").Cells(b, 1).Value And (Worksheets("Sheet2").Cells(b, 2).Value = "Yes" Or Worksheets("Sheet2").Cells(b, 3).Value = "Yes") Then
Worksheets("Sheet1").Cells(a, 1).Font.Color = RGB(0, 176, 240)
Worksheets("Sheet1").Cells(a, 1).Font.Bold = True
End If
Next b
Next a
End Sub
***********************************************