Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
PJ PJ is offline
external usenet poster
 
Posts: 112
Default Conditional formatting using VB and range

I'm trying to update an existing macro to add conditional formatting to a
range of cells. The current macro defines the range of cells to be updated
each time it runs as the range of cells is not always the same. The range of
cells is then named AttRange1.

I used the macro recorder to capture the basic formatting syntax but need
help adding it to the original macro. If the cell begins with the letter "E"
I need to change the font to red bold and this is what it gave me:

Range("J14").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEFT(J14)=""E"""
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
....two similar formatting conditions applied here...
Selection.Copy
Range("J14:Q35").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

In this case, the range begins in J14 and goes to Q35. I set the formatting
in J14 and then used the format painter to apply it to the rest of the range.
If my starting cell was always J14 I think I could get away with changing
the line after the copy command from "Range("J14:Q35").Select" to
"AttRange1.Select" but it is always different.

Is there anyway I can update the code in line 1 and 3 above to use my range
of cells AttRange1?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Conditional formatting using VB and range

This should work if the range has been named at the
point that this snippet is used.


Dim c As Range
For Each c In Range("AttRange1")
If c.FormatConditions.Count < 1 Then
With c.FormatConditions
.Add Type:=xlExpression, _
Formula1:="=Left(" & c.Address & ", 1)=""E"""
With c.FormatConditions(1).Font
.Bold = True
.ColorIndex = 3
End With
End With
End If
Next




"PJ" wrote:

I'm trying to update an existing macro to add conditional formatting to a
range of cells. The current macro defines the range of cells to be updated
each time it runs as the range of cells is not always the same. The range of
cells is then named AttRange1.

I used the macro recorder to capture the basic formatting syntax but need
help adding it to the original macro. If the cell begins with the letter "E"
I need to change the font to red bold and this is what it gave me:

Range("J14").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEFT(J14)=""E"""
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
...two similar formatting conditions applied here...
Selection.Copy
Range("J14:Q35").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

In this case, the range begins in J14 and goes to Q35. I set the formatting
in J14 and then used the format painter to apply it to the rest of the range.
If my starting cell was always J14 I think I could get away with changing
the line after the copy command from "Range("J14:Q35").Select" to
"AttRange1.Select" but it is always different.

Is there anyway I can update the code in line 1 and 3 above to use my range
of cells AttRange1?

  #3   Report Post  
Posted to microsoft.public.excel.programming
PJ PJ is offline
external usenet poster
 
Posts: 112
Default Conditional formatting using VB and range

Thanks JLG. I'm out of the office today so I will test it out next week.

One follow up question though. I have two other conditions I want to add.
So after the last line in your code ("Next"), can I simply re-paste the same
code again for each of the two other conditions? I assume I can just update
the code and use "d" and "e" for the next two conditions along with updating
the formula line.

Thanks again.

"JLGWhiz" wrote:

This should work if the range has been named at the
point that this snippet is used.


Dim c As Range
For Each c In Range("AttRange1")
If c.FormatConditions.Count < 1 Then
With c.FormatConditions
.Add Type:=xlExpression, _
Formula1:="=Left(" & c.Address & ", 1)=""E"""
With c.FormatConditions(1).Font
.Bold = True
.ColorIndex = 3
End With
End With
End If
Next




"PJ" wrote:

I'm trying to update an existing macro to add conditional formatting to a
range of cells. The current macro defines the range of cells to be updated
each time it runs as the range of cells is not always the same. The range of
cells is then named AttRange1.

I used the macro recorder to capture the basic formatting syntax but need
help adding it to the original macro. If the cell begins with the letter "E"
I need to change the font to red bold and this is what it gave me:

Range("J14").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEFT(J14)=""E"""
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.ColorIndex = 3
End With
...two similar formatting conditions applied here...
Selection.Copy
Range("J14:Q35").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

In this case, the range begins in J14 and goes to Q35. I set the formatting
in J14 and then used the format painter to apply it to the rest of the range.
If my starting cell was always J14 I think I could get away with changing
the line after the copy command from "Range("J14:Q35").Select" to
"AttRange1.Select" but it is always different.

Is there anyway I can update the code in line 1 and 3 above to use my range
of cells AttRange1?

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 using a range Marc's NYK Excel Discussion (Misc queries) 1 August 14th 09 04:00 PM
Conditional Formatting Range Jean Excel Discussion (Misc queries) 5 November 29th 07 11:27 PM
Conditional Formatting - Range Monica Excel Programming 3 November 6th 07 11:30 PM
Conditional Formatting Range Donica Excel Programming 2 May 3rd 07 09:39 PM
Conditional Formatting In a Range mikesteven Excel Discussion (Misc queries) 1 July 13th 06 09:37 PM


All times are GMT +1. The time now is 06:05 PM.

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

About Us

"It's about Microsoft Excel"