Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Active cell's column header name using VBA in excel

Tags:

excel

vba

I have a table created from List of data. How to find the header text of each column

Table

When I select the activecell's header is high lighted to orange but I want to retrieve that value using visual basic. I am able to find excel sheet's address but I need table's column header

   Private Sub Worksheet_Change(ByVal Target As Excel.Range)
      MsgBox Target.Value 
      MsgBox ActiveCell.Address
   End Sub
like image 236
kinkajou Avatar asked Oct 04 '11 05:10

kinkajou


People also ask

How to get active cell column in VBA?

VBA Coding Made Easy. If you need to get the active cell’s column or row from VBA, use this code: Active Cell Column. For the cell’s column: 1. 2. 3. PublicSubActiveColumn() MsgBox ActiveCell.

How to reference table column by column header name in Excel?

Reference Table Column by Column Header Name with VBA in Excel 2. Embed VBA to Select Column of a Table without Header by Column Header Name 3. VBA to Insert New Row in the Table with the Reference of Column Header Name 3.1. With the ListColumns Property 3.2. With the Intersect Method 4.

How to create a column header in Excel?

If by column header you mean the cell in the first row in the same column that a change has been made then you can construct the header using Cells, ie Private Sub Worksheet_Change (ByVal Target As Excel.Range) MsgBox Target.Value MsgBox ActiveCell.Address & vbNewLine & "Column header is " & Cells (1, Target.Column) End Sub

How do I find the value of a column in Excel VBA?

Excel VBA Find (Cell with) Value in Table Column To find a cell with a numeric value in an Excel Table column, use the following structure/template in the applicable statement: The following Sections describe the main elements in this structure. A ListColumn object representing the Excel Table column you search in.


2 Answers

This will return the column header, if the passed cell is in a table

Function TableHeader(cl As Range) As Variant
    Dim lst As ListObject
    Dim strHeading As String

    Set lst = cl.ListObject

    If Not lst Is Nothing Then
        TableHeader = lst.HeaderRowRange.Cells(1, cl.Column - lst.Range.Column + 1).Value
    Else
        TableHeader = ""
    End If
End Function
like image 162
chris neilsen Avatar answered Nov 14 '22 21:11

chris neilsen


strCurrentColName = Cells(ActiveCell.ListObject.Range.Row, ActiveCell.Column).Value
like image 43
Mauro Haller Avatar answered Nov 14 '22 22:11

Mauro Haller