VBA for conditional formatting

afrobea_r

Board Regular
Joined
Aug 16, 2015
Messages
76
Dear Excel Community,

I currently have a sheet with data resembling this:

Plan Days
Intended 13
Unintended 12
Intended 14
Intended 15
Intended 16
Unintended 8

<colgroup><col><col></colgroup><tbody>
</tbody>

As the conditional formatting function does not allow application of thick coloured borders, what kind of macro can I use to dynamically apply a thick red border to those cells in the days column only if their corresponding cell value in the plan column is 'unintended'? It must be dynamic as the row data may increase from time to time and certain values within existing rows will also change over time depending on info I have.

I have been stuck for some time. Any help is appreciated!! Thank you in advance!

Best Regards,
stuck
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
Code:
Sub AddBorders()
   Dim Rng As Range
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Replace "Unintended", "=XXX", xlWhole, , False, , False, False
      For Each Rng In .SpecialCells(xlFormulas, xlErrors).Areas
         Rng.Resize(, 2).Borders.Weight = xlThick
         Rng.Resize(, 2).Borders.Color = 45678
      Next Rng
      .Replace "=XXX", "Unintended", xlWhole, , False, , False, False
   End With
End Sub
 
Upvote 0
This is absolutely great sir! Thank u for this! As an additional interface that I just came across, how do I input a formula to cater for a situation whereby there are multiple columns like this:

PlanDaysPlanDaysPlanDaysPlan
Intended666666
Unintended1010 101010
Intended18 181818
Intended4 444
Intended999
Unintended141414
Intended191919191919
Unintended151515151515

<colgroup><col width="64" span="7" style="width:48pt"> </colgroup><tbody>
</tbody>

And the border has to only appear for cell with values as well as tied to the conditional "unintended" in the plan column.

Subsequently, to clear the borders to revert to normal, other than recording a macro, is there a smarter way too?
 
Upvote 0
If the values are constants rather than formulae try
Code:
Sub AddBorders()
   Dim Rng As Range
   Dim Ar As Areas
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Replace "Unintended", "=XXX", xlWhole, , False, , False, False
      Set Ar = .SpecialCells(xlFormulas, xlErrors).Areas
      .Replace "=XXX", "Unintended", xlWhole, , False, , False, False
   End With
   For Each Rng In Ar
      Rng.Resize(, 7).SpecialCells(xlConstants).Borders.Weight = xlThick
      Rng.Resize(, 7).SpecialCells(xlConstants).Borders.Color = 45678
   Next Rng
End Sub
 
Upvote 0
If the values are constants rather than formulae try
Code:
Sub AddBorders()
   Dim Rng As Range
   Dim Ar As Areas
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Replace "Unintended", "=XXX", xlWhole, , False, , False, False
      Set Ar = .SpecialCells(xlFormulas, xlErrors).Areas
      .Replace "=XXX", "Unintended", xlWhole, , False, , False, False
   End With
   For Each Rng In Ar
      Rng.Resize(, 7).SpecialCells(xlConstants).Borders.Weight = xlThick
      Rng.Resize(, 7).SpecialCells(xlConstants).Borders.Color = 45678
   Next Rng
End Sub

Again, brilliant sir. And if the cell values are derived by formula rather constants? How can I make it as if it was the formula option in conditional formatting...
 
Upvote 0
Try
Code:
Sub AddBorders()
   Dim rng As Range
   Dim Ar As Areas
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Replace "Unintended", "=XXX", xlWhole, , False, , False, False
      Set Ar = .SpecialCells(xlFormulas, xlErrors).Areas
      .Replace "=XXX", "Unintended", xlWhole, , False, , False, False
   End With
   For Each rng In Ar
      With rng
         .Borders.Weight = xlThick
         .Borders.Color = 45678
         .Resize(, 7).SpecialCells(xlFormulas, 1).Borders.Weight = xlThick
         .Resize(, 7).SpecialCells(xlFormulas, 1).Borders.Color = 45678
      End With
   Next rng
End Sub
 
Upvote 0
Try
Code:
Sub AddBorders()
   Dim rng As Range
   Dim Ar As Areas
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Replace "Unintended", "=XXX", xlWhole, , False, , False, False
      Set Ar = .SpecialCells(xlFormulas, xlErrors).Areas
      .Replace "=XXX", "Unintended", xlWhole, , False, , False, False
   End With
   For Each rng In Ar
      With rng
         .Borders.Weight = xlThick
         .Borders.Color = 45678
         .Resize(, 7).SpecialCells(xlFormulas, 1).Borders.Weight = xlThick
         .Resize(, 7).SpecialCells(xlFormulas, 1).Borders.Color = 45678
      End With
   Next rng
End Sub


Hi Sir, have tried but when the cell values are derived via formula, the formatting does not apply. How do I recognize the derived value as a value and not a formula?
 
Upvote 0
Hi Sir, have tried but when the cell values are derived via formula, the formatting does not apply. How do I recognize the derived value as a value and not a formula?

Kindly ignore my previous. The formula works fine. However, while it recognizes numerical values derived from formulas, can it also recognize text values that are in the cells derived from formulas as well? eg concatenate.
 
Upvote 0
Kindly ignore my previous. The formula works fine. However, while it recognizes numerical values derived from formulas, can it also recognize text values that are in the cells derived from formulas as well? eg concatenate.

Pardon me once more, I wrongly communicated the last again. What I really meant was that some of the blank cells are derived via a formula and so they are not really blank in that sense. Will the VBA be able to circumvent this and recognize it as a true blank?
 
Upvote 0
The last code I supplied should do that already, although it won't recognise text values.
If you modify it to recognise text values as well as numerical values, then it will pick up any cell with a formula that returns "".
Unfortunately I know of no way round this, other than removing the formulae
 
Upvote 0

Forum statistics

Threads
1,215,126
Messages
6,123,198
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top