Referrring to cells by their column header

  • Thread starter Thread starter Fred Smith
  • Start date Start date
F

Fred Smith

I'm working with an export file that contains several columns each headed by
a description. However, there's no guarantee what order the columns will be
in.

In VBA, is there a way I can refer to a cell, and/or a column, by its
description (much like the sort function does)?
 
Hi Fred

There are as usual different possibilities.

You may have a look at the "Find Method" in the VBA Help.

Or you can search in a range as follows:
Assume you want to search in A1:G1 for a the word "honey"

Sub FindText()
Dim rngX As Range
For Each rngX In Range("a1:g1")
If rngX.Value = "honey" Then rngX.Select 'or whatever you
want to do
Next
End Sub

Best regards

Wolf
 
You can use Find. For instance, the following sets the value of the sixth
cell in the column on the active worksheet whose header (in the first row)
is "abc" to "P".

Rows(1).Find("abc").EntireColumn.Rows(6).Value = "P"

Or set a reference to the column to use later, but check to see if found

Dim rng as Range
On Error Resume Next
Set rng = Rows(1).Find("abc").EntireColumn
If rng is Nothing then 'range not found
'do something
Else
'do something else
rng.Rows(6).Value = "P"
End If
On Error Goto 0

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top