none
rename column header Name

    Question

  • i am using vb.net2010 and working to controling excel.xlsx file how to rename the header column name

    for example   column "A"  to column "Age"

    and thanks for all

    Sunday, September 18, 2011 6:35 PM

Answers

  • Hi Hossam,

    Just like Hans has answered you, this is not possible in Excel.

    But you can do something similar to pretend you have changed column name:

     

    Sub ChangeHeader()
        Dim rng As Range
        Dim sheet As Worksheet
        
        Set sheet = ThisWorkbook.Worksheets(1)
        Set rng = sheet.usedrange
        
        Dim rowCount As Integer
        Dim columnCount As Integer
        
        rowCount = rng.Rows.Count
        columnCount = rng.Columns.Count
        
        Dim desRng As Range
        Set desRng = sheet.Range("B2", sheet.Cells(rowCount + 1, columnCount + 1))
        
        'move down your used range
        rng.Cut desRng
        
        Dim win As Window
        For Each win In Application.Windows
            win.DisplayHeadings = False
        Next win
        
        
        Dim HeadRng As Range
        For i = 2 To columnCount
            Set HeadRng = sheet.Cells(1, i)
            HeadRng.value2 = "Column" & i - 1
        
        Next i
        
        For i = 2 To rowCount
            Set HeadRng = sheet.Cells(i, 1)
            HeadRng.value2 = "Row" & i - 1
        Next i
        
        'also you can change the format of row 1 and column,
        'so that it looks like built-in header
        
    End Sub
    

     


    The code is written via VBA, you can change a little to apply to your VB.NET application.

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 20, 2011 11:42 AM
    Moderator

All replies

  • You cannot change the column letters A, B, C, etc.

    You can enter meaningful column headers in the cells of row 1 of the worksheet.


    Regards, Hans Vogelaar
    Sunday, September 18, 2011 6:50 PM
  • Hi Hossam,

    Just like Hans has answered you, this is not possible in Excel.

    But you can do something similar to pretend you have changed column name:

     

    Sub ChangeHeader()
        Dim rng As Range
        Dim sheet As Worksheet
        
        Set sheet = ThisWorkbook.Worksheets(1)
        Set rng = sheet.usedrange
        
        Dim rowCount As Integer
        Dim columnCount As Integer
        
        rowCount = rng.Rows.Count
        columnCount = rng.Columns.Count
        
        Dim desRng As Range
        Set desRng = sheet.Range("B2", sheet.Cells(rowCount + 1, columnCount + 1))
        
        'move down your used range
        rng.Cut desRng
        
        Dim win As Window
        For Each win In Application.Windows
            win.DisplayHeadings = False
        Next win
        
        
        Dim HeadRng As Range
        For i = 2 To columnCount
            Set HeadRng = sheet.Cells(1, i)
            HeadRng.value2 = "Column" & i - 1
        
        Next i
        
        For i = 2 To rowCount
            Set HeadRng = sheet.Cells(i, 1)
            HeadRng.value2 = "Row" & i - 1
        Next i
        
        'also you can change the format of row 1 and column,
        'so that it looks like built-in header
        
    End Sub
    

     


    The code is written via VBA, you can change a little to apply to your VB.NET application.

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 20, 2011 11:42 AM
    Moderator