excel column names vba code example

Example 1: excel vba Column Name from Column Number

' Column name to column number
ColName = "A"
Debug.Print Range(ColName & 1).Column    ' 1
' Column number to column name
ColNb = 1
Debug.Print Split(Cells(, ColNb).Address, "$")(1)

Example 2: get all column names from excel vba

Sub datacollect()
Dim heading() As String
Dim i As Integer
i = -1
For Each x In Rows(1).Cells
    If x.Value = "" Then Exit For
    i = i + 1
    ReDim Preserve heading(i) As String
    heading(i) = x.Value
Next x
End Sub

Tags:

Vb Example