Changing values in a specific cell based on other cell/row inputs - vba

Hi I'm fairly new to formulas and excel but this is one of the problems I have encountered.
I have been using 2 conditional formatting formulas as follows
=INDIRECT("I"&ROW())="Del"
=INDIRECT("I"&ROW())="Sum"
Where the first formula simply highlights the row grey if del is in the I column of that row, is there also a way of making it change say the K column to 0 if column H in that row is 0?
And for the second formula which also highlights the row another color based on sum input in the given column of that row, is it also possible to change K column of that row to match the value of H column of the given row.
I know they would be similar but I needed to make it so formula one would only zero the K column in the given row if I column had "del" and H column of the row had Zero.
And for the second formula the values would only change in column k of the given row if "sum" was in the I column. Anything else needs to stay unformatted unless these changes are implemented.
I am unable to add a formula the the cells in question as these are overwritten with an button clicked event which inputs data into this field.
any information is appreciated, formula or VBA.

a) use =$I2="Del" instead of INDIRECT (where 2 is the first row of the range your conditional format applies to, e.g. =$A$2:$Z$9999, or the row of the firstly selected cell of the range when you are inserting the conditional format)
b) if you can use a new column that won't be overwritten, the formula in this new column can be:
=if(and(I2="Del";H2=0);0;if(I2="Sum";H2;K2))
P.S.: use , instead of ; if your Windows > Control Panel > Region and Language > Additional settings... > List separator is set to a comma

Just adjust the Offset accordingly to change the column(s) you want.
Dim firstCell As Range
Dim FoundCell As Range
Dim lastrow As Long
With ActiveSheet
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
With Range("H2:H" & lastrow)
Set FoundCell = .Find(What:="3")
Set firstCell = FoundCell
Do Until FoundCell Is Nothing
'.offset(0,-1) would be the same row in Column "G"
FoundCell.Offset(0, -1).Value = 0
'if you wanted to assign the same value then do this:
' FoundCell.Offset(0, -1).Value = FoundCell.Value
Set FoundCell = .FindNext(FoundCell)
If FoundCell.Address = firstCell.Address Then
Exit Do
End If
Loop
End With
End With

Related

Excel: How to color cells based on color of other cells having same value in a range?

I have a range / table of say 10 rows to 3 columns.
Any particular cell can have any particular value from the list created using Data validation. All the cells in this range have the same data validation list.
If a value repeats across any row or across any column, not diagonally or otherwise, then those values are highlighted using conditional formatting.
Now I want that if the same value is present elsewhere in non-duplicate row or column, then those cells should be colored too. (Preferably a different color than one used for conditional formatting to know the difference between both).
The purpose is to know which values are repeated and how, and where in the range those values are used but are not repeated as per the criteria.
P.s.: Please tell if additional information or some clarification is required.
Refer the attached image to understand my query better.
The blue ones are colored through conditional formatting and the green ones are needed to be colored through your help.
Image for understanding
You could use conditional formatting->Duplicate Values for the whole range, with different color and set up sequence in Conditional Formatting->Manage Rules. No need of VBA in my opinion.
EDIT:
Ok, I think I know what are you asking for. Try this little subroutine:
Sub PaintDuplis()
Dim rng As Range
Dim col As Range
Dim row As Range
Dim cl As Range, cl2 As Range
Set rng = Range("B4:D11") 'or whatever your range is.
'Columns
For Each col In rng.Columns
For Each cl In col.Cells
If WorksheetFunction.CountIf(col, cl.Value) > 1 Then cl.Interior.Color = vbYellow
Next cl
Next col
'Rows
For Each row In rng.rows
For Each cl In row.Cells
If WorksheetFunction.CountIf(row, cl.Value) > 1 Then cl.Interior.Color = vbYellow
Next cl
Next row
'Paint whole range
For Each cl In rng
If cl.Interior.Color = vbYellow Then
For Each cl2 In rng
If cl2.Value = cl.Value And cl2.Interior.Color <> vbYellow Then cl2.Interior.Color = vbRed
Next cl2
End If
Next cl
I have given up conditional formatting, instead I have used VBA to paint duplicates in columns/rows and then painted all still white cells with red if it equals to one already in yellow. Hope it helped.

VBA Delete row based on 2 columns

I have some VBA code that will delete the entire row if a cell in a column has red text
Dim Cell As Range
For Each Cell In Intersect(Columns("L"), ActiveSheet.UsedRange)
If Cell.DisplayFormat.Font.ColorIndex = 3 Then Cell.Value = "#N/A"
Next
On Error GoTo NoRedText
Columns("L").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoRedText:
I would like to extend this code to also include another column that includes a "Y" value in the cell.
Column L includes the red text string
Column P includes the "Y" text string
Therefore if the text is red in column L AND the text is equal to "Y" in column P it should delete the entire row
What do I need to add to the code to achieve this?
Thank you
Your Cell variable references a range and all the properties that go with it.
The Offset property
Returns a Range object that represents a range that's offset from the
specified range.
(https://msdn.microsoft.com/en-us/library/office/ff840060.aspx)
Using this knowledge you can tell your code to look at the range that is offset by four columns:
If Cell.DisplayFormat.Font.ColorIndex = 3 AND Cell.Offset(,4)="Y" Then Cell.Value = "#N/A"

Excel macro to compare selected column cell to "next column" cell and color-fill if identical

I'd imagine that this must be very easy to do, but I simply can't find help with my GoogleFu, and I don't understand VBA's object reference guide thing.
What I'm trying to do is make a macro that allows me to select a column, then have each cell in that column compared - row by row - to the cell in the column to its right (e.g. A1:B1 if A1 is selected, F1:G1 is F1 is selected). I don't want a cell compared to all cells in a different column (e.g. A1 compared to all cells in column B), or to all cells in the same row (e.g. A1 compared to all cells in row A).
When I do this manually, I highlight two cells that I want to look at, click "Conditional formatting", do formatting by formula, and enter =x1<>y1 (where x1 is the column I'd select, e.g. A1 or F1, and y1 is the column I want to compare to, e.g. B1 or G1).
I tried using the macro recorder, but it's forcing me to specify columns by ID instead of allowing me to choose a column by selection with my mouse. If there's a way to make a textbox pop up that lets me type in the columns I want, that would work. It would also work if there's a way to populate the selected column as x1 and populate the column to its right as y1. I haven't been able to find info on either of those possibilities. I know that the .Offset property exists, but I don't know how it could be used for what I want.
Here's the code that came from my macro recorder:
Sub ColorHighlightDiscrepancies()
'
' ColorHighlightDiscrepancies Macro
' ColorHighlightDiscrepancies
'
'
Columns("X:Y").Select
Range("Y1").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=X1<>Y1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 4145151
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Here's code that goes row-by-row (found at ExcelForum), but compares all cells within a row to all other cells within the row:
Sub ColorDuplicates()
Dim Data As Variant
Dim DSO As Object
Dim Rng As Range
'Assumes Row 1 has Column Headers
Set Rng = Range("A2").CurrentRegion.Offset(1, 0)
Data = Rng.Value
Set DSO = CreateObject("Scripting.Dictionary")
For I = 1 To UBound(Data, 1)
For J = 1 To UBound(Data, 2)
Key = Trim(Data(I, J))
If Key <> "" Then
If Not DSO.Exists(Key) Then
DSO.Add Key, 1
Else
Rng.Cells(I, J).Interior.ColorIndex = 3
End If
End If
Next J
DSO.RemoveAll
Next I
Set DSO = Nothing
End Sub
If there's a way to make a textbox pop up that lets me type in the columns I want, that would work.
Fortunately, there is:
Dim rng as Range
Set rng = Application.InputBox("Select column(s)", Type:=8)
Alternatively:
I tried using the macro recorder, but it's forcing me to specify columns by ID instead of allowing me to choose a column by selection with my mouse.
Change this:
Columns("X:Y").Select
Range("Y1").Activate
To this (which will select the first cell in the second column of the current Selection):
Range(Selection.Cells(1, 2).Address).Activate

If Statement to check Excel column for a match value

I want to use an If statement (VBA code) to check the cell range in a column for a given numeric parameter. For the cell that matches the given value, the cells at the right (in the same row) should change the background color.
Pseudocode Example:
A1=5,7
If cell in Range(F1:F10) has value=A1 Then
(random matched cell: F7=5,7)
Range (G7:M7) = Background Blue
The part to change the background I know how to do it, but what is the best way to check the given range?
I think you want something like
for i = 1 to 10 'rows in column f to loop through
if cells(i,6) = cells(1,1) then 'column a is 1, column f is 6, etc.
range(cells(i,7), cells(i,13)).interior.colorindex = 'number for that color
end if
next i
I'm guessing you may have multiple rows in F1:F10 that have a match on A1. I would iterate through the cells in the range with:
For each rngCell in Range("F1:F10")
If rngCell.value = Range("A1").value
Range("G" & rngCell.row, "M" & rngCell.row).Interior.ColorIndex = 5
End If
Next

Excel VBA conditional formatting based on next cell value

I am trying to do some conditional formatting in Excel for the font size but seeing as it can't be done from the format menu, it needs to be done with VBA.
I have a range B6 to however many rows and I want to look at the cell next to it and see if it's blank (column C). If it is then format the cell to Bold and 11pt. If it's not blank then it needs to be normal and 9pt.
My code at the minute only makes the last row Bold and 11pt and the rest of the column, even if column C is empty will be normal 9pt.
What is going wrong? BTW I'm using Excel 2003
Dim c As Range, rng
Dim LASTROW As Long
LASTROW = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("B6:B" & LASTROW)
For Each c In rng
If Len(c.Offset(1, 0)) = 0 Then
c.Font.Bold = True
c.Font.Size = 11
Else
c.Font.Bold = False
c.Font.Size = 9
End If
Next c
Your Offset parameters are backwards. You are checking the cell below the current one.
Note the trick is to use a single rule, coded for the top-left cell
This doesn't need a macro - you can do it using a formula in Conditional Formatting.
Say you wanted to highlight the adjacent cell in column B red when the cell in column C had a value of "Red":
=IF(C6="Red",TRUE,FALSE)
then just fill down with the fill handle as usual.
Rule editor (2007):