'********************************************************************************************************** ' 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