coloring cells which have same value - vba

I need to color cells green in column A if their cell value matches cells in column E. cells start from second row. values aren't sorted, so they don't match in rows. I need to check every cell in column A and compare them to every cell in column E.
Please help.

Try following code:
For i = 2 To 100
If ((Worksheets("Table1").Range("A" & i).Value <> "") And (Worksheets("Table1").Range("A" & i).Value = Worksheets("Table1").Range("E" & i).Value)) Then
Range("A" & i).Interior.ColorIndex = 50
Else
Range("A" & i).Interior.ColorIndex = 2
End If
Next i
Just make sure you insert in the second line of the code the name of your table
You can also change the number 100 to the number of rows you want to compare.

Related

How to loop through rows starting in row 2 and find out if row is empty from column E to column AE

Basically I need to figure out how to loop through rows starting with row 2 column E to column AE and to the end of rows, find which ones are empty from column E over to column AE then place a letter code in that rows cell column d and place a different letter code if info is found in any cell in that selection (E thru AE) then of course move down one row and do the same until know info left in rows with column A as a reference on when to stop looping
If anyone can help I'd appreciate it.
Everything I have tried keeps blowing up, can't find a way to assign a value to multiple cell selections, at least that is how I have tried to attack this.
Hope someone can help
This will loop through Column E and if all cells in the active row to Column AE are empty it will put "E" in Column D of the active row, or a "F" in any cell has a value.
For Each cell In Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row)
Dim rng As Range
Set rng = Range("E" & cell.Row & ":" & "AE" & cell.Row)
If Application.WorksheetFunction.CountA(rng) = 0 Then
Range("D" & cell.Row).Value = "E"
Else
Range("D" & cell.Row).Value = "F"
End If
Next cell

Concatenate 2 Columns in VBA

I have 2 columns (column F and G), let's say they both go up to row 10. In row S, I want a concatenate statement: F(G)
I've tried:
SHEET.range("S2:S2" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
and
SHEET.column(18).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"
The problem with either is that after the 10th row, the formula keeps running such that every cell goes "()". How do I edit the macro to end when there's no more data in column F and G?
with:
range("S2:S2" & LastRow)
if LastRow is 10 then the string will be S2:S210 as you have the 2 in the second reference. You want to remove that:
range("S2:S" & LastRow)
So:
SHEET.range("S2:S" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[-13],""("",RC[-12],"")"")"

comparing two cells in certain pattern VBA

I'm trying to compare two excel's cells in certain pattern using VBA. The table is separate by empty rows. And the patter is:
I'm looking at column A. If cell A1 is empty I'm moving to next one. If cell A2 is not empty then I'm compering $A2$ to B2, B3, B4, .. etc until I hit the empty cell. If so, then it moves to the next one (not empty), i.e. $A6$ and I'm continue to compering to B6, B7, ...
For defining the scope (range) of the loop I have the following:
LastRow1 = This.Sheets("List1").Range("A1048576").End(xlUp).Row
Which loop will fit this kind of pattern?
Many thanks in advance.
Correct me if I misunderstood you:
You want to loop through each line in column A till the last row.
Every time you have a value in column A, you want to compare this with the data in column B.
Maybe something like this:
LastRow1 = Range("A" & Rows.Count).End(xlUp).Row 'Determining the last row in column A
LastRow2 = Range("B" & Rows.Count).End(xlUp).Row 'Determining the last row in column B
For i = 2 to LastRow1 'Looping through every row
If Cells(i, "A") <> "" Then
For j = i to LastRow2 'looping through each row in column B from the same line as i in column A at that moment, column A6 f.e. will never compare with a value in B2:B5 for example, but only B6 till lastrow in column B
If Cells(i, "A") = Cells(i, "B") Then
<<do something if the value is found>>
End If
Next j
End If
Next i

Loop Through Columns to Insert SUM Formula

I have a program where I need to find the sum of columns 7 through 30 (G through AD). What I am trying to do is loop through and insert the formula (=SUM(Columns(i)2:Columns(i)1000)) but obviously Columns(i) is not the letter but the number. This is not complying with the format required by SUM so I'm wondering what I can do instead.
I have a program where old sheets will be deleted and new ones added holding configuration data for a product. This means formulas cannot be in the sheet inself or any other sheet referring to it. Prices will be held in columns G through P and U through AD which I need to find the total for and place it in row 1 above the corresponding data. When I try:
For i = 7 To 30
wsNewSheet.Cells(1, i).Value = "=SUM(" & Columns(i).Select & "2:" & Columns(i).Select & "1000)"
Next i
Columns(i) is returning as "True" for some reason. I have also tried to place the totals in a different sheet as the new sheets (wsNewSheet) were being created.
For i = 2 To 30
For f = 7 To 30
wsTotals.Cells(1, i).Value = "=SUM(" & wsNewSheet & "!" & Columns(f).Select & "2:" & Columns(f).Select & "1000)"
Next f
Next i
However, this did not work either. This statement returned "Run-time error '438.' Object doesn't support this property or method." I attemped to do research on this error, but could not fix my situation. Thanks for any help.
You don't need loop here, use single line instead:
Range("G1:AD1").Formula = "=SUM(G2:G1000)"
Excel automatically adjust formulas:
in G1 you'd have =SUM(G2:G1000)
in H1 you'd have =SUM(H2:H1000)
....
in AD1 you'd have =SUM(AD2:AD1000)

Remove single quote VBA function

To simplify, I have two sheets in an Excel workbook.
If I write =sheet2!R3C2 in one of the cells of sheet1, the cell takes the value of the cell R3C2 of sheet2 correctly.
I would like to generalize that for many rows in VBA, so my code is:
row = XX a long
temp = "=sheet2!R" & row - 1 & "C2"
Range("c" & row).Value = temp
But when I use this code, the value of the cell(row,3) is =sheet2!'RXXC2'
How can I remove the single quotes ??
Range("C" & row).Formula = temp
would produce the correct formula in your cell.
What you should consider doing instead of looping is
Range("A1").Formula = "=Sheet2!$B1"
Range("A1").Resize(100, 1).Formula = Range("A1").Formula
The first line inserts a formula =Sheet2!$B1 in cell A1 of your active sheet. The $ dollar sign assures that the column will not be incremented (same applies with numbers)
Then the second line duplicates the formula across 100 rows down from A1 replacing the number after the B column
So now
A1 =Sheet2!B1
A2 =Sheet2!B2
A3 =Sheet2!B3
...
Also, it's a bit unclear what you're trying to actually do so consider another option which is saving the value of formula into another range using the Evaluate() function
Range("c" & row).Value = Evaluate(temp) Or Range("C" & Row).Value = [temp]
try to write Range("c" & row).FormulaR1C1=temp
You want to set the formula, not the value:
Range("c"&row).FormulaR1C1 = temp