Programming Samples

Click here to go to:

Excel VBA

Word VBA

MS Access




Power BI

Crystal Reports


SQL Replication

C# Code


Oracle PL/SQL

Database Diagramming

Back to Home Page

Automating Excel Column Headers

How to Automate Column Header Titles in Excel

Reports are not usually generated one time only. Typically, they are run over and over again on a monthly, weekly or daily basis. Performing this task manually is a complete waste of time better spent learning new coding techniques!

Column headers can be either copied and pasted or generated dynamically.

Data from SQL Server inserted into Excel sheet:Excel Data

Worksheet Header contains the Column Headers to be inserted above.Copy Header Technique

Excel Copy and Paste Version of VBA Code

This code in a VBA Macro will simply copy and paste the row with the header from a different sheet (called Header, here) and paste it on our Data sheet after Inserting a new Row 1. As long as this information does not change, this is a quick way to get the job done.

Public Sub CopyHeader()
  Selection.Insert Shift:=xlDown
End Sub

Excel VBA Dynamic Column Header Names

This code will create an array of columns (letters + row1) to populate the headers from an ADODB recordset using a for loop. If the column names will change in the data source, it may be easier to manage the column headers dynamically as showe here:

Public Const DB_NAME As String = "AdventureWorks"
Public Const source As String = "Sales.usp_SalesPerformance"
Public Const GLOBAL_DB_CXN_STRING = "Provider=MSDataShape;Data Provider=SQLOLEDB;SERVER=####;DATABASE=" & DB_NAME & ";Integrated Security=SSPI"

Public Sub DynamicHeader()
  Dim rs As ADODB.Recordset
  Dim cn As ADODB.Connection
  Dim cmd As ADODB.Command
  Dim rng As Range
  Dim stCell As Integer, iRecCount As Integer
  Dim i As Integer, j As Integer
  Dim stFields() As String
  Dim arrSize As Integer

On Error GoTo errHandler

  stCell = 1
  arrSize = 8

   ReDim Preserve stFields(arrSize)

'populate the array with capital letters using ASCII codes 65 to 90 or less

  For i = stCell To arrSize
   stFields(i) = Chr(64 + i)
  Next i
  Set cn = New ADODB.Connection
  cn.ConnectionString = GLOBAL_DB_CXN_STRING
  Set cmd = New ADODB.Command
  Set rs = New ADODB.Recordset

  With cmd
   .CommandType = adCmdStoredProc
   .CommandText = source
   .CommandTimeout = 300
   .ActiveConnection = cn
   'Connection object
   Set rs = .Execute
  End With

Do Until (rs.State = adStateOpen)
  Set rs = rs.NextRecordset

  ActiveWindow.Zoom = 80

'Test to see if we have records
If Not (rs.BOF And rs.EOF) Then
'Copy recordset to the range
  Set rng = ThisWorkbook.ActiveSheet.Range("a1")
  rng.CopyFromRecordset rs
End If

  'insert header row
  'populate the header names
  For j = stCell To arrSize
   Range(stFields(j) & "1").Select
   ActiveCell.FormulaR1C1 = rs.Fields(j - 1).Name
  Next j

  'set the header names to bold
  Selection.Font.Bold = True

   'freeze the top row
   ActiveWindow.FreezePanes = True

  'close out of our recordset and connections
  Set rs = Nothing
  Set cmd = Nothing
  Set cn = Nothing

  Exit Sub

  errmsg = "Error: " & Err.Number & ":" & Err.Description
  MsgBox errmsg, vbExclamation
  Resume ExitHandler

End Sub

The Column Names from the Recordset are populated by iterating through an array to get the ADODB.Recordset .Name property and populate Row 1 using ASCII Char numbers 65 through 90 to get the letter into an array. Of course, if you exceed the alphabet limit, there is a trick to get the rows populated. Some automated column formatting would also help.

Data with Headers