ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Selecting Columns by Title (https://www.excelbanter.com/excel-programming/346972-selecting-columns-title.html)

Jessica

Selecting Columns by Title
 
I am writing a macro to select columns based on their title.

If I always wanted to select Column D, then I could use:
Columns("D:D").Select

But in my case, I want to select a column that is titled "RC". I tried to
replace "D:D" with "RC", but I get an error.

Thanks,
Jessica

Bill Kuunders

Selecting Columns by Title
 
You could name the columns. <Insert<name<add
for instance RCcolumn refers to D:D
and then use the line

Application.Goto Reference:="RCcolumn"


--
Greetings from New Zealand
Bill K
"Jessica" wrote in message
...
I am writing a macro to select columns based on their title.

If I always wanted to select Column D, then I could use:
Columns("D:D").Select

But in my case, I want to select a column that is titled "RC". I tried to
replace "D:D" with "RC", but I get an error.

Thanks,
Jessica




Dave Peterson

Selecting Columns by Title
 
Does that mean you have the RC characters in a cell in row 1 and want to select
that column?

Option Explicit
Sub testme()
Dim FoundCell As Range
Dim WhatToFind As String

WhatToFind = "RC"

With Worksheets("Sheet1")
With .Rows(1)
Set FoundCell = .Cells.Find(What:=WhatToFind, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=True)
End With
End With

If FoundCell Is Nothing Then
MsgBox "No " & WhatToFind & " found in row 1"
Exit Sub
End If

FoundCell.EntireColumn.Select

End Sub


Jessica wrote:

I am writing a macro to select columns based on their title.

If I always wanted to select Column D, then I could use:
Columns("D:D").Select

But in my case, I want to select a column that is titled "RC". I tried to
replace "D:D" with "RC", but I get an error.

Thanks,
Jessica


--

Dave Peterson


All times are GMT +1. The time now is 11:26 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com