Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 237
Default If then statement

Below is part of a code that works, but I need to make it
conditional. I would like to have the code perform this
function:

If the value "area1" is found anywhere in Range Z1:Z26,
perform the code.
Range("area1").Interior.ColorIndex = 32

How would I write this?

Thank you.
Todd Huttenstine
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default If then statement

Try:

'--------------------------------

Dim rng as range
set rng = activesheet.range("Z1:Z26")

dim c as range
for each c in rng.cells
if c.value = "area1" then
c.interior.colorindex = 32
else
c.interior.colorindex = 1 '??? or whatever
end if
next

'---------------------------------


That should give you a starting point.

--
Michael Hopwood (Phobos)


"Todd Huttenstine" wrote in message
...
Below is part of a code that works, but I need to make it
conditional. I would like to have the code perform this
function:

If the value "area1" is found anywhere in Range Z1:Z26,
perform the code.
Range("area1").Interior.ColorIndex = 32

How would I write this?

Thank you.
Todd Huttenstine



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default If then statement

One way:

with worksheets("sheet1")
if application.countif(.range("z1:z26"),"area1") 0 then
.range("area1").interior.colorindex = 32
end if
end with

(I put area1 on sheet1. Modify if required.)

This actually looks for "area1" in the cell.
It won't react to "this is area1 here".

But you could use:
if application.countif(.range("z1:z26"),"*area1*") 0 then

If the range gets large, then using .find might be quicker.

Dim FoundCell As Range
With Worksheets("sheet1")
Set FoundCell = Nothing
Set FoundCell = .Range("z1:z26").Find(what:="area1", _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)

If FoundCell Is Nothing Then
'do nothing
Else
.Range("area1").Interior.ColorIndex = 32
End If
End With

Todd Huttenstine wrote:

Below is part of a code that works, but I need to make it
conditional. I would like to have the code perform this
function:

If the value "area1" is found anywhere in Range Z1:Z26,
perform the code.
Range("area1").Interior.ColorIndex = 32

How would I write this?

Thank you.
Todd Huttenstine


--

Dave Peterson

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
IF statement inside a SUMIF statement.... or alternative method Sungibungi Excel Worksheet Functions 3 December 4th 09 06:22 PM
Reconcile Bank statement & Credit card statement & accounting data Bklynhyc Excel Worksheet Functions 0 October 7th 09 09:07 PM
Embedding an OR statement in an IF statement efficiently Chatnoir11 Excel Discussion (Misc queries) 4 February 2nd 09 08:12 PM
appending and IF statement to an existing IF statement spence Excel Worksheet Functions 1 February 28th 06 11:00 PM
Help please, IF statement/SUMIF statement Brad_A Excel Worksheet Functions 23 January 11th 05 02:24 PM


All times are GMT +1. The time now is 12:16 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"