+ Reply to Thread
Results 1 to 6 of 6

Convert a dynamic column number into letter within a vba formula

  1. #1
    Registered User
    Join Date
    09-11-2009
    Location
    MN
    MS-Off Ver
    Office 2010
    Posts
    48

    Convert a dynamic column number into letter within a vba formula

    In my workbook, users click a button which updates a worksheet. One of the things that I would like it to do is put in a number of formulas in the active row starting in column G and in every column until at least column BG.

    This is the code that I was trying to use and it works in the sense that it puts the formula in all the correct rows and columns. The only problem that I have with it are the two places where I'm referencing "ActiveCell.Column" because it puts in the column number instead of the column letter. How can I change this so that it will reference the column letter?


    Please Login or Register  to view this content.
    Thank you!
    Last edited by XLVBA; 05-14-2015 at 11:19 AM.

  2. #2
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Convert a dynamic column number into letter within a vba formula

    There are a couple of ways.

    First, you can convert the the column number to the ASCII letter number equivalent (by adding 64), then convert that ASCII number to a letter using Chr(). like so:

    Please Login or Register  to view this content.

    This works fine for single letter columns, but when you get up to column AA and beyond, you start having to account for the additional letter.

    I think a more elegant approach is to split the address. If ActiveCell is A1, then ActiveCell.Address = "$A$1". You can split this string at the dollar signs, then grab the first position. This will grab the column letter regardless of how many letters it is. Like so:

    Please Login or Register  to view this content.
    Again, the second approach is much more robust, and since you mentioned column BG, i would say it's definitely the way to go.

  3. #3
    Registered User
    Join Date
    09-11-2009
    Location
    MN
    MS-Off Ver
    Office 2010
    Posts
    48

    Re: Convert a dynamic column number into letter within a vba formula

    The second approach is exactly what I needed. Thank you!

  4. #4
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Convert a dynamic column number into letter within a vba formula

    Glad to help. If you're satisfied with the solution, any reputation points (the * in the lower left of my posts) is appreciated.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Convert a dynamic column number into letter within a vba formula

    I know this has been marked "solved". I would suggest, if only for educational purposes, another approach. One of the challenges, especially in VBA, of using A1 notation, is that formula strings change as they are copied around when you are using relative references. One of the advantages, especially in VBA, of R1C1 notation is that formula strings do not change as they are copied and moved around.

    In your case, for example (assuming I follow correctly), the formula string in R1C1 notation would be "...AND(R2C>=RC5,R2C<=RC6)...". Note that, in this approach, there is no need to convert column numbers to letters. There would be no need to reference column number at all (except to specify which cell is receiving the formula).

    If you are satisfied with the A1 solution you have, then ignore this post. I would suggest that, if you are going to be doing a lot of this kind of programming, that you will be well served to become familiar with R1C1 notation and the .FormulaR1C1 property.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Registered User
    Join Date
    09-11-2009
    Location
    MN
    MS-Off Ver
    Office 2010
    Posts
    48

    Re: Convert a dynamic column number into letter within a vba formula

    Being protected, things don't really get moved around so the A1 solution works great. I'm sure there will be future workbooks where this will definitely be more of a concern so thank you very much for providing an alternative and the explanation. I've only started to familiarize myself with R1C1 so this was a great reminder to give that a shot. Thank you both!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Convert Column Number to Letter VBA
    By amotto11 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-18-2012, 06:01 PM
  2. Convert Column number to letter...
    By zeph77 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2012, 09:56 AM
  3. Convert column number to letter
    By gtmeloney in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-08-2009, 11:13 AM
  4. How to convert column number to letter
    By iturnrocks in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-21-2007, 09:51 AM
  5. [SOLVED] Convert Column letter to number
    By slymeat in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-27-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1