Macro to Add Column Name instead of Headers

AriffChowdhury

New Member
Joined
Jul 28, 2013
Messages
10
I have a macro that does the following;

Code:
Sub CreateHeaders()

    Dim wsData As Worksheet
    Dim wsHeaders As Worksheet
    Dim headerRange As Range
    Dim header As Range
    Dim i As Long: i = 0
    Dim anchor As Range
    Dim subAddr As String


    Set wsData = Worksheets("Sheet1")
    Set wsHeaders = Worksheets("Sheet2")
    Set headerRange = wsData.Range("A1", wsData.Range("A1").End(xlToRight))
    Set anchor = wsHeaders.Range("B5")  '## begin inserting the hyperlinks at B5


    For Each header In headerRange  '## iterate over each cell in the header row
        subAddr = "'" & wsData.Name & "'!" & header.Address
        With wsHeaders
            .Hyperlinks.Add anchor:=anchor, Address:="", SubAddress:= _
                 subAddr, TextToDisplay:=header.Value
        End With
        i = i + 1
        Set anchor = anchor.Offset(1, 0) '## increment the location of the next hyperlink, to the next row
    Next
End Sub

It copies the header name but i want that to copy COLUMN NAME Like "COLUMN A","COLULMN B","COLUMN C" and so on...

Thank you!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi and welcome to Mr Excel Forum

I made some changes (in blue)

See if this is what you want

Code:
Sub CreateHeadersNew()
    Dim wsData As Worksheet
    Dim wsHeaders As Worksheet
    Dim headerRange As Range
    Dim header As Range
    Dim i As Long: i = 0
    Dim anchor As Range
    Dim subAddr As String
    [COLOR=#0000ff]Dim colLetter As String[/COLOR]
   
    Set wsData = Worksheets("Sheet1")
    Set wsHeaders = Worksheets("Sheet2")
    Set headerRange = wsData.Range("A1", wsData.Range("A1").End(xlToRight))
    Set anchor = wsHeaders.Range("B5")  '## begin inserting the hyperlinks at B5

    For Each header In headerRange  '## iterate over each cell in the header row
        subAddr = "'" & wsData.Name & "'!" & header.Address
        
        [COLOR=#0000ff]With wsHeaders.Columns(header.Column)
            colLetter = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
        End With
[/COLOR]     
        With wsHeaders
            .Hyperlinks.Add anchor:=anchor, Address:="", SubAddress:= _
                 subAddr, TextToDisplay:=[COLOR=#0000cd]"Column " & colLetter[/COLOR]
        End With
        i = i + 1
        Set anchor = anchor.Offset(1, 0) '## increment the location of the next hyperlink, to the next row
    Next
End Sub

M.
 
Upvote 0
Dear Marcelo Branco
This is exactly what I needed for..
Thank you so much for your reply!

And a little question for my own knowledge......I have another question should I start new thread for this ?
 
Upvote 0
Dear Marcelo Branco
This is exactly what I needed for..
Thank you so much for your reply!

And a little question for my own knowledge......I have another question should I start new thread for this ?

You are welcome and thanks for the feedback.

About your question:
If the new question is not related with the macro above i suggest a new thread.

M.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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