• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / VBA / How to Get Column Letter from Number Using an Excel VBA Macro

How to Get Column Letter from Number Using an Excel VBA Macro

December 11, 2020 Leave a Comment

When dynamically creating formulas using an Excel VBA macro, being able to switch between the column letter and numbers in a loop is very important.

For example, let’s say you are looping over a number of columns in a Excel VBA macro. In the example, below, it’s possible I want to create a dynamic sum of the first row based on an input, and store that in a formula.

My program could be structured as follows:

Sub CreateSum() 

Dim colNumber as integer
Dim input as integer
Dim sumstr as string

input = 5

For colNumber = 1 To input
    columnLetter = Split(Cells(1, colNumber).Address, "$")(1)
    If colNumber = input Then
        sumstr = sumstr &  columnLetter & "1"
    Else 
        sumstr = sumstr &  columberLetter & "1,"
    End If
Next

ThisWorkbook.Sheets("Example").Cells(1,input+1).Formula = "=SUM(" & sumstr & ")"
End Sub

With one line of code, we can easily obtain the column letter given a number for the column.

columnLetter = Split(Cells(1, colNumber).Address, "$")(1)

If you would like to go the other way, from column letter to column number, you would want the following:

columnNumber = Range(columnLetter & 1).Column

Other Articles You'll Also Like:

  • 1.  VBA Asin Worksheet Function – Find Arcsine and Inverse Sine of Number
  • 2.  VBA Atanh Worksheet Function – Inverse Hyperbolic Arctangent of Number
  • 3.  VBA Trig – Using Trigonometric Functions in VBA for Trigonometry
  • 4.  VBA Acosh Worksheet Function – Inverse Hyperbolic Arccosine of Number
  • 5.  VBA Cos – Find Cosine of Number in Radians Using VBA Cos() Function
  • 6.  VBA Convert Radians to Degrees with WorksheetFunction.Degrees()
  • 7.  VBA e – Using Exp() Function to Get Euler’s Constant e
  • 8.  VBA Atn – Find Arctangent and Inverse Tangent of Number
  • 9.  VBA Sin – Find Sine of Number in Radians Using VBA Sin() Function
  • 10.  VBA Tanh Worksheet Function – Find Hyperbolic Tangent of Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy