Macros in excel: Find Similarities between two columns and fill the coincidences - excel

I want a macro that search in a column and find coincidences with the other column the fill it with yellow to recongnize it.
Column A 1137 Rows
Colimn B 537 Rows
Fill with yelllow in A the values found in B
I thinks its like
For i = 1 To i = 1137
For n = 1 To n = 592
If Cells(A, i) = Cells(L, n) Then
Cells(i, A).Value = Fill
End If
Next
Next

Why not use conditional formatting instead of a macro? Picture's worth more than my typing...
Original post didn't have a negative result so I added one.

You can do this VERY easily with conditional formatting!
Just select all of column A where you want the data to be highlighted and, then:
Select Conditional Formatting
New Rule
Use a Formula to determine which cell to format
Put in the following conditional format rule:
=ISNUMBER(MATCH($A1,$B$1:$B$537,0))
Set the fill color to yellow
And that should do it!

Related

Filtering for Multiple Colors in One Column in Excel

I've been having issues conditionally formatting a sheet 2 (Shipping Request List) that is mirroring select columns of sheet 1 (Master List).
-Starting in sheet 2 (Shipping Request List), column B, cell B2, has ='Master List'!D2 (from sheet 1) and continues down for hundreds of rows.
-The data in this field is mirroring cell 'Master List'!D2 (sheet 1) which could either be "Open" (or a number of other items based off a drop down list... no formulas). I'm only interested in what happens when the cell says, "Open".
-Cell D2 in sheet 1 has the following conditional formats based on D2 saying "Open" and a date in cell V2 in the Master List sheet.
=AND($V2<TODAY(),$D2="open") [turns purple]
=AND($V2-TODAY()>=0, $V2-TODAY()<=2,$D2="open") [turns red]
=AND($V2-TODAY()>=3, $V2-TODAY()<=4,$D2="open") [turns orange]
=AND($V2-TODAY()>=5, $V2-TODAY()<=7,$D2="open") [turns yellow]
When I try conditional formatting based on the values in sheet 2, i.e. by the value "Open" mirrored from sheet 1, the colors are not correct.
I tried various approaches.
Approach 1:
Change the conditional format equations in sheet 2 to reference the cell in sheet 1 instead of directly referencing the cells in sheet 2.
Approach 2:
Try using INDIRECT in the conditional format equations in sheet 2...but I'm not sure I did this correctly.
Approach 3:
Use a VBA code to create a UDF on sheet 1 to extract the color code of column D into an adjacent column (C), then filter by that number for the codes that represent purple, red, orange, and yellow.
(I am not at all good with VBA, but I can copy and paste a module and follow instructions.)
Function FindColor(n As Range) As Integer
FindColor = n.Interior.ColorIndex
End Function
In column C, C2 had =findcolor(D2) and then that got pulled down. This approach gave me numbers, but it gave me the same number for purple, blank, etc... extremely inconsistent.
Approach 4:
In new column C, add a formula to make the words either "purple", "red", "orange", or "yellow" appear based on the conditions mentioned in the conditional formatting formulas. I got only so far as the formula started getting a little too complicated for me to get through it. Only got this far (didn't even add orange and yellow yet) but the formula below is not returning "red" when the conditions call for it:
=IF(AND($V662<TODAY(),$D662="open"),"purple",IF(AND($V662-TODAY()>=0,$V662-TODAY()<=2,$V662="open"),"red",""))
I think I was getting close on the last approach, but I realize it's a lot of formula and will be added to 700+ rows, which will continue to grow.
As a slightly different approach: you could use a UDF to return the "color" for each row, and then run your conditional formatting rules based on the return values.
Put this in a regular module:
Function Classify(theDate, theStatus) As String
If theStatus = "open" Then 'check status
'If theStatus = "open" Or theStatus = "issue" Then
If Len(theDate) > 0 Then 'has a date?
Select Case theDate - Date
Case Is < 0: Classify = "purple"
Case Is >= 5: Classify = "yellow"
Case Is >= 3: Classify = "orange"
Case Is >= 0: Classify = "red"
End Select
End If
End If
End Function
In your worksheet you'd use (eg)
=Classify($V662, $D662)

Excel Conditional Formatting with Two Conditions

I am trying to format a row to change color based on the value of the cells at the end of the row. For whatever reason, Excel is forcing the Applies to to contain additional $ and the cells are not highlighting in any logical way.
Here is what I am trying to do:
If Column I = Yes & Column J = Yes -> Row "Fill Color" is Green
If Column I = No & Column J = Yes -> Row "Fill Color" is Orange
If Column I = No & Column J = No -> Row "Fill Color" is Red
The formulas I am using are as follows:
=OR(I$3<>"Yes",J$3<>"Yes") Format: Fill Red Applies to =A$3:D$3
=OR(I$3="No",J$3<>"Yes") Format: Fill Orange Applies to =A$3:D$3
=OR(I$3="Yes",J$3="Yes") Format: Fill Green Applies to =A$3:D$3
Excel keeps changing the Applies to to be =A$3:$D$3 and even though I keeping changing it to =A$:D$3 I can't seem to find why.
Currently, here is the behavior I am seeing:
Column A is Orange and columns B-D are staying Red when I & J are Yes
Columns A-D are Red when and J is No regardless of what column I is
Columns A-D do not turn Green in with any combination
Am I missing something really simple? I can't seem to figure out why it would work this way.
Two things...
$ Sign
I believe the $ behavior you are seeing is normal... The formulas can be relative or absolute cells, but the ranges to which the rules apply should not be and would not have any reason to be.
Logic
I believe your logic problem will be embarrassingly obvious once you step back for a moment and look at your formulas. In your text version of your logic you describe your logical condition as AND(). That is, if I is <> "yes" AND J is <> "yes". But take a look at your formulas.
You didn't enter:
=AND(I$3<>"Yes",J$3<>"Yes")
but rather you entered:
=OR(I$3<>"Yes",J$3<>"Yes")
Change OR to AND and I suspect you are back in business.

Conditional color coding in Excel

I have a fairly simple question regarding Excel and VBA, but because I'm new to VBA, I have no idea how to implement it.
I have an entire column full of a color code. The cells in that column say either R, G, or Y.
I need a macro that will traverse that column, change all cells that say "R" to have a fill of red, change all cells that say "Y" to have a fill of yellow, and change all cells that say "G" to have a fill of green.
I know this can also be done by simply sorting the rows alphabetically and then changing the colors manually, but I am looking for an automatic way of doing that.
Just use conditional formatting. You can change both the text and the cell to be the same colour
Highlight the column with your R, Y, and G and apply conditional formatting. | Home tab > Conditional Formatting > Highlight Cell Rules > Equal to... | Enter your letter of choice and select the corresponding color scheme!
Please try this:
Range("A1").Interior.Color = RGB(0, 0, 0)`
For R = 0 To 255
For G=0 To 255
For B=0 to 255
Cells(1 , 1).Interior.Color = RGB(R, G, B)`
Next R
You can use another loop change Cells row and column.

Background formatting pairs of rows in a spreadsheet

I was needing a way to format alternating pairs of rows in a worksheet with the same background color. When looking around I found the below link:
Excel VBA: Alternate Row Colors in Range
My problem is similar except that instead of wanting every other row auto filled with a background color I need adjacent pairs of rows colored. For instance, from my start point at row 4 columns A:T would be filled, row 5 columns A:T would have the same background color, rows 6 and 7 columns A:T would be without background color, rows 8 and 9 would share the background color as rows 4 and 5, repeated until the end of the spreadsheet.
I've made an attempt at using conditional formatting for this purpose but 1) I had not been able to get the background alternating for every pair of rows from my start point and 2)it overrides the few special cases that have a different background color 3) conditional formatting does not allow me to manually format any rows that the conditional formatting function formatted.
Many thanks to the commenters for their suggestions (which put me on the right track) but due to the limitations of Conditional Formatting I cobbled together the following Macro that allows the background to be formatted to my needs without eliminating the ability to correct for special cases. The code is heavily commented to help other newbies to understand what the code means and what to modify to change the behavior of the Macro.
Sub AlternateRowColors()
''PURPOSE: To format the background color of alternating pairs of rows in a designated range of cells with values.
'' A correction to account for a possible empty row at the end of the range not having a value failing to follow the
'' desired pattern is included.
'' Example: Column A
'' Row 1: Green
'' Row 2: Green
'' Row 3: No Background Color
'' Row 3: No Background Color
'' Repeating this pattern until the end of the used cells of a worksheet.
Dim lastUsedRow As Long ''Variable to hold the last row number.
lastUsedRow = Range("A200").End(xlUp).Row ''This checks backwards from cell A200 to A1 for the last row with an entry in Column A
''and saves the row number in variable lastUsedRow. Modify this as needed for each worksheet.
If lastUsedRow Mod 2 = 0 Then ''This block of code corrects for the second row of an entry not being highlighted at the
lastUsedRow = lastUsedRow + 1 ''end of the worksheet if the first cell in the following row is blank.
Else
End If
For Each cell In Range("A4:T" & lastUsedRow) ''Sets the range of rows and columns that the background color formatting is to affect.
If cell.Row Mod 4 = 0 Then ''Highlight row if the row number has a divided remainder of zero
cell.Interior.ColorIndex = 35 ''Sets background color by using .ColorIndex instead of RGB. Change to suit your need.
Else
If cell.Row Mod 4 = 1 Then ''Highlight row if the row number has a divided remainder of 1
cell.Interior.ColorIndex = 35 ''Sets background color by using .ColorIndex instead of RGB. Change to suit your need.
End If
End If
Next cell
End Sub
''ADDITIONAL NOTES:
''NONE
Try these formulas in conditional formatting > using a formula to determine which cell's to format:
=AND(ROW()>3,MOD(ROW(),4)=1)
and
=MOD(ROW(),4)=0
both would apply to $A:$T
Put your rules for specially formatted cells after these general rules.
Hope this helps.
In conditional formatting use the formula
=Mod(Row(),4) < 2
in the cells you want the rules to apply to

Excel: Custom a conditional format rule: if cells in colums C are not empty, then

I am trying to use a custom conditional format rule: if cells in column C are not empty, then color all other cells of the same line in green.
What should I enter in the cell of the Conditional Format Rule option? (http://hpics.li/9492511)
Should I use something like "not blank"?
Assuming headers are in row 1 and actual data starts in row 2, use this conditional format formula:
=AND($C2<>"",COLUMN(A2)<>3)
Apply it to the range that contains your data, in this example, I applied it to range $A$2:$E$21 and here is an image of what the result would look like:
Conditional formatting only works for the same cell, unfortunately.
You can use VBA to check the cell, and format other ones based on that formatting.
for(i = 0 to NUM_ROWS)
if cells(i,3).value = ""
for(j = 0 to NUM_COLS)
cells(i,j).Interior.Color = RGB(33, 173, 28)
next
end if
next
I haven't tested it, but it should be pretty close.
Actually, it looks like you could do this with conditional formatting.
for your formula you'd want
$C1 = ""
This should work for every value.

Resources