Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Member
 
Posts: 40
Default Conditional formatting within macro?

I know how to use conditional formatting within the excel sheet, but I want to integrate it into a macro (the macro is doing other things as well). Is this possible?

Basically, there is a column for the age of a car. The macro is pulling the data and pasting it into a new workbook - but when it's pasted/as it's pasted, I want anything older than 2008 (age in column Q) to become red (the cell that is). Is this possible?

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Conditional formatting within macro?

Hi Jonathan,

Am Tue, 26 Mar 2013 14:39:17 +0000 schrieb JonathanK1:

I know how to use conditional formatting within the excel sheet, but I
want to integrate it into a macro (the macro is doing other things as
well). Is this possible?

Basically, there is a column for the age of a car. The macro is pulling
the data and pasting it into a new workbook - but when it's pasted/as
it's pasted, I want anything older than 2008 (age in column Q) to become
red (the cell that is). Is this possible?


implement to your existing code:

Dim LRow As Long

LRow = Cells(Rows.Count, "Q").End(xlUp).Row
Range("$Q$2:$Q$" & LRow).Select
With Selection
.FormatConditions.Add Type:=xlExpression, Formula1:="=$Q2<2008"
.FormatConditions(.FormatConditions.Count).SetFirs tPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End With


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3   Report Post  
Member
 
Posts: 40
Default

Quote:
Originally Posted by Claus Busch View Post
Hi Jonathan,

Am Tue, 26 Mar 2013 14:39:17 +0000 schrieb JonathanK1:

I know how to use conditional formatting within the excel sheet, but I
want to integrate it into a macro (the macro is doing other things as
well). Is this possible?

Basically, there is a column for the age of a car. The macro is pulling
the data and pasting it into a new workbook - but when it's pasted/as
it's pasted, I want anything older than 2008 (age in column Q) to become
red (the cell that is). Is this possible?


implement to your existing code:

Dim LRow As Long

LRow = Cells(Rows.Count, "Q").End(xlUp).Row
Range("$Q$2:$Q$" & LRow).Select
With Selection
.FormatConditions.Add Type:=xlExpression, Formula1:="=$Q2<2008"
.FormatConditions(.FormatConditions.Count).SetFirs tPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End With


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
It acted like it was going to work and then stopped, highlighting the ".FormatConditions(.FormatConditions.Count).SetFir st Priority" section.

Doh! I appreciate your help :)

J-
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Conditional formatting within macro?

Hi Jonathan,

Am Wed, 27 Mar 2013 10:45:27 +0000 schrieb JonathanK1:

It acted like it was going to work and then stopped, highlighting the
".FormatConditions(.FormatConditions.Count).SetFir st Priority" section.


do you have other conditional formattings in your sheet? What Excel
version do you use?
Try:

Sub test()
Dim LRow As Long

LRow = Cells(Rows.Count, "Q").End(xlUp).Row
Range("$Q$2:$Q$" & LRow).Select
With Selection
.FormatConditions.Add Type:=xlExpression, Formula1:="=$Q2<2008"
With .FormatConditions(1).Interior
.ColorIndex = 3
End With
End With
End Sub

and if it doesn't work, try:

Sub test2()
Dim LRow As Long
Dim rngC As Range

LRow = Cells(Rows.Count, "Q").End(xlUp).Row
For Each rngC In Range("$Q$2:$Q$" & LRow)
rngC.Interior.ColorIndex = IIf(rngC < 2008, 3, xlNone)
Next
End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #5   Report Post  
Member
 
Posts: 40
Default

Quote:
Originally Posted by Claus Busch View Post
Hi Jonathan,

Am Wed, 27 Mar 2013 10:45:27 +0000 schrieb JonathanK1:

It acted like it was going to work and then stopped, highlighting the
".FormatConditions(.FormatConditions.Count).SetFir st Priority" section.


do you have other conditional formattings in your sheet? What Excel
version do you use?
Try:

Sub test()
Dim LRow As Long

LRow = Cells(Rows.Count, "Q").End(xlUp).Row
Range("$Q$2:$Q$" & LRow).Select
With Selection
.FormatConditions.Add Type:=xlExpression, Formula1:="=$Q2<2008"
With .FormatConditions(1).Interior
.ColorIndex = 3
End With
End With
End Sub

and if it doesn't work, try:

Sub test2()
Dim LRow As Long
Dim rngC As Range

LRow = Cells(Rows.Count, "Q").End(xlUp).Row
For Each rngC In Range("$Q$2:$Q$" & LRow)
rngC.Interior.ColorIndex = IIf(rngC < 2008, 3, xlNone)
Next
End Sub


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
The first one worked perfectly. Thank you!!!!

J-


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Conditional formatting within macro?

Hi Claus,
I need to put a Formula in an expression as you showed above.
My issue is that I need manage the columns as a number and not a letter. Ranges have to be dynamic in columns.

If I write:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=SUM($I$5:$N$5)0"

it works properly, but

Selection.FormatConditions.Add Type:=xlExpression, FormulaR1C1:="=SUM(R5C9:R5C14)0"

it doesn't.

Can you give me any help on it?
Thx
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Conditional formatting within macro?

Hi Claus,
many thanks for your answer.

Turning the Formula type from FormulaR1C1 to Formula! and using ".Address(0,0)" at least no error is returned.

But now the CF is not effective (cells painted when the condition is true) even I can see the rule set on the CF Manager.

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Conditional formatting within macro?

Hey Claus,
thanks for your help, now it works great.

The point was the clause ".Address(0/1,0/1)" which lets me put the $'s wherever is needed.
I fixed my issue by:

..FormatConditions.Add Type:=xlExpression, Formula1:="=SUMA(" & Cells(5, 9).Address(1, 1) & ":" & Cells(5, 14).Address(1, 1) & ")0"

where columns now are available to be managed as numbers, thus variables.

Regarding the language, my excel is Spanish. I had no need to use "FormulaLocal", what you see above is enough.

Thanks again,
Jordi
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Formatting & Macro MickyRat Excel Discussion (Misc queries) 0 May 27th 10 10:05 PM
Conditional Formatting Macro Daren Excel Discussion (Misc queries) 2 June 27th 07 10:58 PM
Conditional Formatting in a Macro Ed Excel Discussion (Misc queries) 2 August 28th 06 11:23 PM
Conditional Formatting Macro dok112[_3_] Excel Programming 2 June 14th 04 02:06 PM
macro for conditional formatting No Name Excel Programming 8 October 27th 03 08:03 PM


All times are GMT +1. The time now is 10:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"