ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find Column to use based on Name of Header (https://www.excelbanter.com/excel-programming/378807-re-find-column-use-based-name-header.html)

Otto Moehrbach

Find Column to use based on Name of Header
 
You would first have to define the range of headers. Then search that range
for the header text using Find. That will give you a column number that you
can assign to a variable, say ColNum. Then your code has to be re-written
to something like:
Set Rng = Range(Cells(2,ColNum), Cells(2,ColNum).End(xlDown)).
Please post back if this doesn't work for you or if you need more. HTH
Otto
"DJS" wrote in message
...
Hello~

~~~~~~~~~~~~~~
Here is a snippet of my code:

Set rng = Range(Range("J2"), Range("J2").End(xlDown))
~~~~~~~~~~~~~~

Instead of referencing Column J (as above), I would like to find the
correct
Column based on the name of the column header (in this case it is "VIN").

Thanks.




Otto Moehrbach

Find Column to use based on Name of Header
 
Don
This macro sets the range of the column header row (assuming it's row 1)
and asks for the column header to search for, then finds the column number
of the column that has that column header.
Watch out for line wrapping in this message. The line that starts with "Set
RngHeaders" ends with "xl(ToLeft)).
The line that starts with "ColNum=" ends with "Column".
Perhaps it would be more helpful to you if you send me your file (or a file
that looks like your file) so I can see the whole task you are trying to
accomplish. I need just the layout of your data, not the actual data. My
email address is . Remove the "nop" from this address.
HTH Otto
Sub GetColNum()
Dim ColNum As Long
Dim RngHeaders As Range
Dim ColHeader As String
'Set the rng of the column headers
Set RngHeaders = Range("A1", Cells(1, Columns.Count).End(xlToLeft))
'Get the column header to search for
ColHeader = InputBox("Enter the column header to find.")
'Find the column number
ColNum = RngHeaders.Find(What:=ColHeader, LookAt:=xlWhole).Column
End Sub

"DJS" wrote in message
...
Thanks for the assistance Otto, but I can't seem to figure out what method
or
property to use to fetch the column number.
I understand I need to search the first row for my header name, but how do
I
extract the column number?
Could you possibly point me in the correct direction?

TIA
--Don

"Otto Moehrbach" wrote:
You would first have to define the range of headers. Then search that
range
for the header text using Find. That will give you a column number that
you
can assign to a variable, say ColNum. Then your code has to be
re-written
to something like:
Set Rng = Range(Cells(2,ColNum), Cells(2,ColNum).End(xlDown)).
Please post back if this doesn't work for you or if you need more. HTH
Otto
"DJS" wrote in message
...
Hello~

~~~~~~~~~~~~~~
Here is a snippet of my code:

Set rng = Range(Range("J2"), Range("J2").End(xlDown))
~~~~~~~~~~~~~~

Instead of referencing Column J (as above), I would like to find the
correct
Column based on the name of the column header (in this case it is
"VIN").

Thanks.








All times are GMT +1. The time now is 12:13 PM.

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