VBA Conditional Formatting

Mo796

New Member
Joined
Mar 12, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I am not sure how to achieve my desired result and have not yet found a code combination that works. I'm pretty much learning as I go and have not used conditional formatting in VBA yet. I am tracking balances from one tab (Balance Entry Sheet) to another tab (Balance Timelines). The current macro shifts all cells down and pastes the newly collected data on the new first line of the receiving tab. I want the most recent capture to show on the top row of the new page for convenience, which the macro currently accomplishes. However, my issue is that I currently have column H on the receiving tab conditionally formatted to calculate the difference between the current cell and the cell below it in column F and fill the cell color in column H either green or red and corresponding with a value "+" or "-" based on whether the calculation determines the value has gone up or down. IE "=IF((F3-F4)>0,"+","-")" and so on. Since my macro creates a new line when copying and pasting the data from another tab I lose the conditional formatting calculation and end up with a blank cell in row H. How do I best ensure this conditional formatting is also applied to the new row? Below is my current macro.


Sub Track()

Sheets("Balance Timelines").Select

Rows("3:3").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrBelow = 0

Dim sourceWs As Worksheet, dstWs As Worksheet


With Range("B3")

Set sourceWs = Sheets("Balance Entry Sheet")
Set dstWs = Sheets("Balance Timelines")

sourceWs.Range("B3").Copy

Call dstWs.Range("A4").End(xlUp).Offset(1).PasteSpecial(Paste:=xlPasteValues)

End With

With Range("B4")

Set sourceWs = Sheets("Balance Entry Sheet")
Set dstWs = Sheets("Balance Timelines")

sourceWs.Range("B4").Copy

Call dstWs.Range("B4").End(xlUp).Offset(1).PasteSpecial(Paste:=xlPasteValues)

End With

With Range("B5")

Set sourceWs = Sheets("Balance Entry Sheet")
Set dstWs = Sheets("Balance Timelines")

sourceWs.Range("B5").Copy

Call dstWs.Range("C4").End(xlUp).Offset(1).PasteSpecial(Paste:=xlPasteValues)

End With

With Range("B6")

Set sourceWs = Sheets("Balance Entry Sheet")
Set dstWs = Sheets("Balance Timelines")

sourceWs.Range("B6").Copy

Call dstWs.Range("D4").End(xlUp).Offset(1).PasteSpecial(Paste:=xlPasteValues)

End With

With Range("D25")

Set sourceWs = Sheets("Debt")
Set dstWs = Sheets("Balance Timelines")

sourceWs.Range("D25").Copy

Call dstWs.Range("E4").End(xlUp).Offset(1).PasteSpecial(Paste:=xlPasteValues)

End With

With Range("G4")

Set sourceWs = Sheets("Future Projections")
Set dstWs = Sheets("Balance Timelines")

dstWs.Range("G4").End(xlUp).Offset(1).Value = Now()

End With

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello,

After running your code add in the following.


VBA Code:
Sub FormatCells()
    If Range("F3").Value - Range("F4").Value > 0 Then
    Range("H3").Value = "+"
    Range("H3").Interior.Color = 5287936
    Else
    Range("H3").Value = "-"
    Range("H3").Interior.Color = 255
    End If
End Sub

Since the VBA doesn't change it will always look at cells F3 & F4 and colour H3

Jamie
 
Upvote 0
Solution
Hello,

After running your code add in the following.


VBA Code:
Sub FormatCells()
    If Range("F3").Value - Range("F4").Value > 0 Then
    Range("H3").Value = "+"
    Range("H3").Interior.Color = 5287936
    Else
    Range("H3").Value = "-"
    Range("H3").Interior.Color = 255
    End If
End Sub

Since the VBA doesn't change it will always look at cells F3 & F4 and colour H3

Jamie
As my macro is running on a different worksheet I just added Sheets("Balance Timelines").Select under Sub and I can have it on both worksheets. Thank you for assisting me!
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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