View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Cush Cush is offline
external usenet poster
 
Posts: 126
Default how to select a Row based on contents of a Cell

a couple of ways can be used --

Using CF:
-Select rows 1-100 with A1 as the active cell
-Click on FormatCF
-Change the Value box to Formula is
- with your cursor in the equals box select cell $G$1
- click on the F4 (function) key to change the relative property to $G1
-add to this making it =$G1="Apple"
-Click on the Format button and the Pattern Tab and select Red
Click OK a couple of times to exit CF
Any row in row 1 to 100 with "Apple" in col G should now be red .

This will not work for Deleting a row. You must do this either manally or
with vba code, something like the following. Tip: start from the bottom of
your range and work your way up the column of interest looking for the word
or condition. If/when the condition is found then delete the entire row:

Option Explicit

Sub DeleteOhio()
Dim oCell as Range
dim i as integer

For i=100 to 2 Step -1
oCell = Cells(i,7) ' Col G = 7th col
if oCell.value = "Ohio" then
oCell.EntireRow.Delete
end if
Next i

End Sub


This will delete any rows (2-100) that has "Ohio" in col G.

The same vba approach can be used to color a row:
oCell.EntireRow.interior.colorindex=3


"swintronix" wrote:

Hello all... first-time poster here, with what I hope is a simple issue to
bounce off the group. I can't find information about this anywhere. It
isn't specifically addressed in any of the VBA resources I know of....

I believe this is similar to conditional formatting, but I'm trying to work
with entire rows, rather than just cells. Here's what I'm trying to do,
using VBA...

"If any cell in Column X contains value=Y, then perform Z on the entire
Row."

Examples:

If any cell in Column G contains the word "Apple", then the entire Row
containing that cell shuld be Red.

or

If any cell in Column D contains the word "Ohio", then delete the entire
Row.

Can expression be written in VBA? It seems like conditional formatting, but
it's not a "per cell" condition. Any suggestions will be greatly
appreciated.

- Scott