![]() |
If Statement
I am trying to write a macro that selects a row if a value in a specified
column meets a certain criteria. For example, I have a column entitled "YEAR". If the value in this column is 2005, then I want to select the entire row. I am able to identify the column and put the cursor in the first cell below the column header, but I don't know what to write an If Then statement that will highlight the row if the value is 2005. The othe problem is that I want to make this macro generic because the YEAR column is not always in the same location. Thanks, Jessica |
If Statement
Assuming we are talking about sheet2 and your headings are in row 1 this
is one way that uses the find method: Sub GetRow() Dim yrHead As Range Dim yrFnd As Range With Sheets(2) With .Rows(1) Set yrHead = .Find("Year") End With If Not yrHead Is Nothing Then With .Columns(yrHead.Column) Set yrFnd = .Find(2005) End With If Not yrFnd Is Nothing Then .Rows(yrFnd.Row).EntireRow.Select End If End If End With End Sub Hope this helps Rowan Jessica wrote: I am trying to write a macro that selects a row if a value in a specified column meets a certain criteria. For example, I have a column entitled "YEAR". If the value in this column is 2005, then I want to select the entire row. I am able to identify the column and put the cursor in the first cell below the column header, but I don't know what to write an If Then statement that will highlight the row if the value is 2005. The othe problem is that I want to make this macro generic because the YEAR column is not always in the same location. Thanks, Jessica |
All times are GMT +1. The time now is 12:19 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com