Creating a Procedure to Call HeaderName but no success to call a procedure for HeaderName’s Column Position

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello

Was trying to create a Procedure to Call Header Name and it Column position

Got stuck as I could not put together to get the column Reference of Column Header Name.

In module

Call HeaderNameColPosName("Header9")
Call ColPosof(“Header9”)

What needs to be coded for Exact Col pos of ColHeader to derive it succesffuly to get the correct Column Reference when procedure called for “Header9” like above
I need to call another procedure for the Column Reference for above
So when

Call ColPosof(“Header9”)

I should get as colPosofHeader9.Column

And then I can utilise wherever required like below


For eg

as wsRng.Columns(ColPosofHeader9.Column).Address(0,0)

VBA Code:
Private Sub HeaderNameColPosName(SrchHeaderName As String)
Dim SrchtxtHeaderName As String
Dim ColFindHeaderName As Range
Public ColNamePos As String
Dim ColumnLetter As String

With Worksheets("Sheet1").Range("A1:J1")
               SrchtxtHeaderName = SrchHeaderName
               Set ColFindHeaderName = .Find(What:=SrchtxtHeaderName, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
               ColumnLetter = Replace(Cells(1, ColFindHeaderName.Column).Address(0, 0), 1, "")
               ColNamePos = ColumnLetter
            ‘’’’  MsgBox SrchHeaderName & " is with Col.Name " & ColNamePos
End With
End Sub

Your help will be highly appreciated
Thanks SamD
88
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
One idea maybe would be to make a function of the code to return the column number

Something like following

VBA Code:
Function HeaderNameColPosName(ByVal SrchHeaderName As String) As Variant
    Dim arr As Variant
   
    arr = Worksheets("Sheet1").Range("A1:J1").Value2
             
    HeaderNameColPosName = Application.Match(SrchHeaderName, arr, 0)

End Function

and to test idea

Code:
Sub atest()
    Dim HeaderName, whichcolumn
   
    HeaderName = "Header9"
   
    whichcolumn = HeaderNameColPosName(HeaderName)
'name not found
    If IsError(whichcolumn) Then Exit Sub
   
    MsgBox HeaderName & " is with Column " & Columns(whichcolumn).Address & " Column Number " & whichcolumn
End Sub

If suggestion is what you are looking for you could add another parameter to function to pass the Range - this would allow you to use it on other worksheets / ranges.

Hope Helpful

Dave
 
Upvote 0
Thanks Dave(dmt32) for a quick response
I would like to know
arr = Worksheets("Sheet1").Range("A1:J1").Value2 for what purpose you have used value2

If suggestion is what you are looking for you could add another parameter to function to pass the Range - this would allow you to use it on other worksheets / ranges.
How can you do the above

Will it be not possible to use
Call aTest("Header9")
I ran above it gave me invalid outside ..... error
89
 
Upvote 0
Thanks Dave(dmt32) for a quick response
I would like to know
arr = Worksheets("Sheet1").Range("A1:J1").Value2 for what purpose you have used value2

The difference between Range.Value and Range.Value2 is the data types they work with -

Range.Value can return formatted Currency and date

Range.Value2 returns just the underlying value which includes empty, string, error, number and Boolean

How can you do the above

Just add a range parameter

VBA Code:
Function HeaderNameColPosName(ByVal Target As Range, ByVal SrchHeaderName As String) As Variant
    Dim arr As Variant
    
    arr = Target.Value2
              
    HeaderNameColPosName = Application.Match(SrchHeaderName, arr, 0)

End Function

You then pass the range to it

Code:
whichcolumn = HeaderNameColPosName(Worksheets("Sheet1").Range("A1:J1"), HeaderName)

Will it be not possible to use
Call aTest("Header9")
I ran above it gave me invalid outside ..... error
89

You can do that but would need to place the Variable in code as a parameter to pass value to the code

Code:
Sub atest(ByVal HeaderName As String)
    Dim whichcolumn
    
    
    whichcolumn = HeaderNameColPosName(Worksheets("Sheet1").Range("A1:J1"), HeaderName)
'name not found
    If IsError(whichcolumn) Then Exit Sub
    
    MsgBox HeaderName & " is with Column " & Columns(whichcolumn).Address & " Column Number " & whichcolumn
End Sub


Dave
 
Upvote 0
I tried as per your post #5 and this time code was executed in userform added the below command button
It worked till Calling atest("Header7") but also tried to get Columns(whichcolumn).Address - raised the error Application Defined Error.......
the reason to get reference of .Columns(whichcolumn).Address is to work with that column (Whichcolumn)
this is to get exactly as per my #1
ie
Call aTest("Header7")
.Columns(whichcolumn).Address or Calling whichColumn("Header7")
End Sub
VBA Code:
Private Sub CommandButton1_Click()
   Dim whichcolumn
   Call atest("Header7")
   Worksheets("Sheet1").Cells(2, 7).Value = Worksheets("Sheet1").Columns(whichcolumn).Address
End Sub
91
 
Last edited:
Upvote 0
I tried as per your post #5 and this time code was executed in userform added the below command button
It worked till Calling atest("Header7") but also tried to get Columns(whichcolumn).Address - raised the error Application Defined Error.......
the reason to get reference of .Columns(whichcolumn).Address is to work with that column (Whichcolumn)
this is to get exactly as per my #1
ie
Call aTest("Header7")
.Columns(whichcolumn).Address or Calling whichColumn("Header7")
End Sub
VBA Code:
Private Sub CommandButton1_Click()
   Dim whichcolumn
   Call atest("Header7")
   Worksheets("Sheet1").Cells(2, 7).Value = Worksheets("Sheet1").Columns(whichcolumn).Address
End Sub
91


If want to use code in that manner then couple changes required

In the atest code you need to include the whichcolumn variable as an argument

Rich (BB code):
Private Sub CommandButton1_Click()
   Dim whichcolumn
   Call atest("Header7", whichcolumn)
   Worksheets("Sheet1").Cells(2, 7).Value = Worksheets("Sheet1").Columns(whichcolumn).Address
End Sub


In the atest code itselef, also needs to be added as a parameter

Rich (BB code):
Sub atest(ByVal HeaderName As String, ByRef whichcolumn As Variant)
    
    whichcolumn = HeaderNameColPosName(Worksheets("Sheet1").Range("A1:J1"), HeaderName)
'name not found
    If IsError(whichcolumn) Then Exit Sub
    
    MsgBox HeaderName & " is with Column " & Columns(whichcolumn).Address & " Column Number " & whichcolumn
End Sub

This should return the value in whichcolumn to calling procedure.

Dave
 
Upvote 0
Dave Thank you so much for your wonderful help and explanation. This has really encouraged me to develop functions and procedures for which i had less confidence.
Indeed your solution was perfect to the need.
Really Grateful to you and appreciate your time and efforts spend for explanation and solution. ?

SamD
92
 
Upvote 0
Many thanks for your kind feedback – very much appreciated:)



Dave
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top