Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default Using Column Name

The following function when used will return the column number where Row1
has the text passed to the function. If it is not found then the function
returns zero.

Function FindHeader(sHeader As String)
' take the string sHeader and return column
' number for the first column with sHeader in row 1
' else returns a 0
Dim iCol As Integer, iColumns As Integer
FindHeader = 0
With ActiveSheet
iColumns = .Cells(1, .Columns.Count).End(xlToLeft).Column
For iCol = 1 To iColumns
If Trim(.Cells(1, iCol)) = Trim(sHeader) Then
FindHeader = iCol
Exit Function
End If
Next
End With
End Function

To use it put the function in a general module and then call it by using

myColumn = FindHeader("Product Line")

The variable myColumn will have the number of the column where "Product
Line" is in row1. The function uses the activesheet so it looks at the sheet
currently selected.

HTH


--

Regards,
Nigel




"Lucky Lucy" wrote in message
...
Thank you Nigel. The problem I have is that I cannot reference by column
letter, as that column letter will vary on different worksheets. What I
would
like to do is reference by column heading/title, so I can use the same
macro
on different worksheets.

Thanks,
Luce

"Nigel" wrote:

To copy an entire column try something like, you can reference it by
column
letter if you fully reference it with the name of the sheet

The following copies the entire column D on sheet1 to column A on sheet2

Sheets("Sheet1").Columns("D:D").Copy
Destination:=Sheets("Sheet2").Range("A1")

--

Regards,
Nigel




"Lucky Lucy" wrote in message
...
Hi,
How do I copy a column that is called "Product Line" into Column A? The
Product Line column is in a different column on different reports so I
cannot
reference it by column letter and hope to reference it by column
heading/name.

Thank you!
Luce




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referencing date column A & time column B to get info from column TVGuy29 Excel Discussion (Misc queries) 1 January 24th 08 09:50 PM
Search for a column based on the column header and then past data from it to another column in another workbook minkokiss Excel Programming 2 April 5th 07 01:12 AM
Based on a condition in one column, search for a year in another column, and display data from another column in the same row look [email protected] Excel Programming 2 December 30th 06 06:23 PM
Based on a condition in one column, search for a year in another column, and display data from another column in the same row look [email protected] Excel Discussion (Misc queries) 1 December 27th 06 05:47 PM
How can i have all alike product codes in column A be matched with like cities in column B and then add the totals that are in column C [email protected] Excel Programming 4 August 2nd 06 01:10 AM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"