Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > How to program conditional format dynamically


Reply
 
Thread Tools Display Modes
  #1  
Old 05-14-2008, 02:50 PM
jzd jzd is offline
Newcomer
 
Join Date: May 2008
Posts: 1
Default How to program conditional format dynamically


Hi,
I would like to be able to program the Formula in VBA dynamically but not sure how to make the .add function accept a dynamic string. E.g.

for each cell in range("a1:a10")
' i'd like to set the formula to not equal to the next cell, e.g.
' for cell A1, Formula = B1
' for cell A2, formula = B2, etc

FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual,Formula1:=???

next cell

VBA doesn't allow a string that's not a proper field name to be passed.
Any suggestions? Thanks much!



Edit by Moderator:
Please post Excel questions, in the Excel forum.

Thank you.

Last edited by Timbo; 05-15-2008 at 02:31 AM.
Reply With Quote
  #2  
Old 05-14-2008, 08:30 PM
Cas's Avatar
Cas Cas is offline
Senior Contributor

* Expert *
 
Join Date: May 2008
Posts: 1,012
Default

Hi jzd, welcome to the forum!

The formula string to pass must be of the form "=B1" if you want it to refer to the value in cell B1, as opposed to the literal string "B1". So what you need to do in your approach is to
  • navigate to the next cell, e.g. using .Offset
  • get its address, using .Address
  • prepend a "=" to that string
There is actually an easier way to do this, though - you can use the INDIRECT worksheet function with a relative R1C1 cell reference, e.g. the formula
Code:
=INDIRECT("R[-1]C",FALSE)
will set a cell equal to the value of the cell above. And since this uses relative coordinates, you can copy the formula to another cell and it'll refer to the cell above that one, without the need for updating the reference. Using that, you can set the conditional formatting for the entire range without the need to loop through the cells.

Hope I didn't misunderstand what you want to do.
Reply With Quote
  #3  
Old 05-15-2008, 01:53 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline
Out Of Office

Retired Moderator
* Expert *
 
Join Date: Mar 2005
Location: London, UK
Posts: 3,398
Default

Hi jzd and welcome to the forum!

Please be sure to read the posting guidelines.

You can just directly do it like this without looping and without copying:
Code:
Range("A1:A10").FormatConditions.Add _
                        Type:=xlCellValue, _
                        Operator:=xlEqual, _
                        Formula1:="=$B1"
I obtained it just by tidying up the code produced by the macro recorder.
Try it with Not Equal and see what you get.

Colin
__________________
RAD Excel Blog
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->