Dealing with column headers

  • Thread starter Thread starter Pawan
  • Start date Start date
P

Pawan

Hi...

I am learning about writing macros. I always stuck up at one point.
I need to do various activies on columns based on their headings. e.g. I
have to replace all "NA" in the column whose heading is "TOWN". Currently I
write the macro using column number. But if the columns gets shifted, then
this macro will not work. How can I extract column number by identifying its
heading?

Regards,
Pawan
 
try this

for x = 1 to columns.count
if columns(x).cells(1).value2 = YourValue then exit for
next x

x is then the column number so you would reference as such columns(x)

To go one step further you could wrap this in a function

public function get_column(byval YourValue as string) as integer
for x = 1 to columns.count
if columns(x).cells(1).value2 = YourValue then
get_column = x
exit function
end if
next x
end function

HTH
 

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