Excel VBA conditional formatting based on next cell value - vba

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):

Related

Saving number as text, how to automate it

I have a 7702216772 number inside a cell. If I put a ' before the fist digit and click Enter Excel transforms the number to a text and puts a green triangle at the left top of the cell:
I have many rows of similar numbers all of which need to be transformed into text. However clicking each and adding ' before the first symbol and clicking Enter would take a lot of time. Is there any way to do it programatically?
I tried using formula: ="'"&H4 but it doesn't do what's expected - the green triangle never appears on the result cell.
I also tried setting cell format to Text, but the green triangle doesn't appear in that case too.
I need the green triangle to appear at the upper left corner, just like at the picture!
If all your number are in a single column, the following code will do it:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = 1 To LastRow 'loop from row 1 to last
ws.Cells(i, "A").Value = "'" & ws.Cells(i, "A").Value 'add the ' before the number
Next i
End Sub
Change the "A" to whichever column you are using.
Just Select the cells you wish to process and run this short macro:
Sub Textify()
Dim rng As Range, r As Range
Set rng = Selection.Cells.SpecialCells(2, 1)
For Each r In rng
r.Value = "'" & r.Value
Next r
End Sub
Non VBA answer; I'm using Column G in this answer but it depends on where your numbers are. You'll have to change the cell but I think you will be ok with this.
In an empty cell, enter formula: ="'"&G4
Use the fill handle or Ctrl+D to fill it down to the length of Column G's values.
Select the whole of Column G's values and copy them to the clipboard
Select the same range in Column G, right-click, select Paste Special and choose Values
I have tested it now for several times and it worked always
Cells(xx, xx).FormulaR1C1 = "'" & Cells(xx, xx).Value
Same would work for ActiveCell or whatever you like.

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.

How to skip through selected cells in EXCEL, using VBA,

I have a VBA macro which selects several cells based on if it contains conditional formatting. These cells won't all be in the same place on each sheet.
What I am looking for is a command to skip the activecell to the next cell in the range.
The same as pressing TAB on a highlighted range
At the moment I am using sendkeys, as below, however this is messy, and keeps adding Tab spaces in the next line of the vba code (hence the "____Loop")
ActiveCell.SpecialCells(xlCellTypeAllFormatConditions).Select
Do Until Recount = Count
Recount = Recount + 1
Application.SendKeys "{TAB}", True
Loop
Any advice would be appreciated
Here's how you can loop over the range:
Dim rng As Range, c As Range
Set rng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeAllFormatConditions)
For Each c In rng
c.Select
Next c
It's not clear what the aim of your code is though. What are Count and Recount?
Get a list of selected cells and loop through them
Sub loopThroughCells()
Dim r as Range
Set r = Application.Selection
For i = 0 to r.length
MsgBox(r.value)
Next i
End Sub
Suppose three cells with values 1, 2 and 3 are selected. On running the above macro, you will get message boxes with the values 1, 2 and 3 respectively.
If you only need the command for the tab button, just use the .offset(#of rows you want to offset, #of columns you want to offset). So once you know how to locate the cells you need, which you seem to already have, then you can just put.offset(0,1) to move one cell to the right.

IF THEN VBA MACRO - Update one column if contents of another = 100%

I have a workbook with "Results" being sheet 3, this being the worksheet I want to use.
I have tried a few formulaes to try and add a macro to do the following:
I have column G with percentages. I then have column I where I would like there to be a result saying TRUE/FALSE where the contents of G are equal to 100%. Column G is formatted to percentage with two decimals.
Some considerations: I have my first row being a Hyperlink to another sheet, then my headings, then the first row of "results". I have 457 rows, if there is a measurement of the range, perhaps it could be on A?
I keep getting this error 9 with my range and have got a bit stuck.
Thanks in advance!
Sub PartialHits1()
Dim rng As Range
Dim lastRow As Long
Dim cell As Range
With Sheet3
lastRow = .Range("G" & .Rows.Count).End(xlUp).Row
Set rng = .Range("G1:G" & lastRow)
For Each cell In rng
If cell.Value = 100
Then
cell.Range("I1:I1").Value = 100
End If
Next
End With
End Sub
(I have hacked this a bit, just was trying to get it to set as 100 instead of the TRUE/FALSE Also was playing around near the Sheet 3 part as I got errors.)
RangeVariable.Range can refer only to a cell within RangeVariable, so you can't refer to column I in this way. Try: .Range("I"&cell.row)=100.
Also your criteria is probably wrong, if you have 100% in a cell it's actual value is 1.
And last question: why do you want to do this with VBA, it would be much more simple with worksheet function =IF(G3=1,100,"")

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

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