I am working on a section of a fairly large VBA macro that is designed to apply conditional formatting when another cell is equal to 1, so that the cell can be highlighted as ‘selected.’
When generating the conditional formatting formula that does this, excel seems to be changing constant values of its own accord – all the values it reads from should be at row 99 and this is in the code, but excel instead changes the formula to =IF(B101=1,1,0) for the first one, then =IF(B105=1,1,0) for the next one and so on.
These are merged cells, so I would assume that excel is trying to autocomplete my code by offsetting the formula by how many rows down I am. Obviously this is not desired, does anyone know how I can stop excel from doing this? The relevant section of code is below:
For Each x In Range(crange)
condrow = 99
Range(Cells(coffset + 3, y + 1), Cells(coffset + 2 + currentdiv, y + 1)).FormatConditions.Delete
Cells(coffset + 3, y + 1).Value = x
selectform = "=IF(" & ConvertToLetter(Cells(coffset + 3, y + 1).Column) & CStr(condrow) & "=1,1,0)"
Range(Cells(coffset + 3, y + 1), Cells(coffset + 2 + currentdiv, y + 1)).Merge
Range(Cells(coffset + 3, y + 1), Cells(coffset + 2 + currentdiv, y + 1)).FormatConditions.Delete
Range(Cells(coffset + 3, y + 1), Cells(coffset + 2 + currentdiv, y + 1)).FormatConditions.Add Type:=xlExpression, Formula1:= _
"clashform"
I believe you need to prefix a row or column reference by a “$” to prevent Excel from automatically adjusting it for different rows or columns.
For example, “B$101” will allow the column (B) to be adjusted as the formula moves to a different location, but not the row, which is preceeded by a “$”.