Column header


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'**********************************************************************************************************
' Name:                 columnHeader
' Author:               mielk | 2012-02-22
'
' Comment:              Returns the header of a column with the specified index number.
'
' Parameters:
'   columnIndex         Index of column which header is to be returned.
'                       It should be integer greater than 0. If the passed value is less or equal to 0,
'                       NegativeColumnIndexException is raised.
'
' Returns:
'   String              Header of a column with the specified index number.
'                       If the given column index is less than 1, an empty string is returned.
'                       Function accepts a much greater range of indices than number of columns in any of
'                       existing Excel version. That is because of possibly increased capacity of
'                       worksheets in the future Excel versions.
'
'
' Exceptions:
'   NegativeColumnIndexException    Thrown if the given column index is negative or equal to 0.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2012-02-22        mielk           Method created.
'**********************************************************************************************************
Public Function columnHeader(columnIndex As Long) As String
    Const METHOD_NAME As String = "columnHeader"
    '------------------------------------------------------------------------------------------------------
    Const ALPHABET_LENGTH As Byte = 26
    Const ASCII_BEFORE_A As Byte = 64
    '------------------------------------------------------------------------------------------------------
    Dim lTemp As Long
    Dim iLetter As Integer
    '------------------------------------------------------------------------------------------------------


    'The function checks if the given column index is less than or equal to 0. In such case, ------------|
    'the code execution moves to NegativeColumnIndexException label.                                    '|
    If columnIndex <= 0 Then GoTo NegativeColumnIndexException                                          '|
    '----------------------------------------------------------------------------------------------------|


    '----------------------------------------------------------------------------------------------------|
    lTemp = VBA.Int(columnIndex)                                                                        '|
    Do                                                                                                  '|
        iLetter = lTemp Mod ALPHABET_LENGTH                                                             '|
        If iLetter = 0 Then iLetter = ALPHABET_LENGTH                                                   '|
        columnHeader = VBA.Chr$(iLetter + ASCII_BEFORE_A) & columnHeader                                '|
        lTemp = Excel.WorksheetFunction.RoundUp((lTemp - ALPHABET_LENGTH) / ALPHABET_LENGTH, 0)         '|
    Loop While lTemp > 0                                                                                '|
    '----------------------------------------------------------------------------------------------------|



'==========================================================================================================
ExitPoint:
    Exit Function

'----------------------------------------------------
NegativeColumnIndexException:
    '(...)
    'Put your own error handling here for a case if the given index number is negative or 0.

    GoTo ExitPoint

End Function