Link to home
Start Free TrialLog in
Avatar of Miss leah
Miss leahFlag for Israel

asked on

excel range address by numbers

I need to translate a column number at runtime to an excel range address in vb.net.
is there any function I can use instead of writing my own?
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

If I understand you correctly, this is how I do it is C#
        private static CellCoordinates ParseA1Notation(string cellAddr)
        {
            CellCoordinates returnValue = new CellCoordinates();
            try
            {
                Dictionary<string, int> row_col = new Dictionary<string, int>();
                int ii = 0, jj, colVal = 0;
                while (cellAddr[ii++] >= 'A') { };
                ii--;

                for (jj = 0; jj < ii; jj++) colVal = 26 * colVal + cellAddr[jj] - 'A' + 1;
                returnValue.x = colVal;
                returnValue.y = Int32.Parse(cellAddr.Substring(ii));
            }
            catch (Exception ex)
            {
                logger.Error($"Error \"{ex.Message}\" parsing \"{cellAddr}\"");
            }
            return returnValue;
        }

Open in new window

Avatar of Miss leah

ASKER

Thank you Shaun.  can you tell which librarys do I need to add ?
Probably just System.Collections.Generic and CellCoordinates is just a class
        private class CellCoordinates
        {
            public int x;
            public int y;
        }

Open in new window

trying to convert to vb.net im getting an error "operator - is not defined for types char and char"
on    this:
             colVal = 26 * colVal + (cellAddr(jj) - "A"c) + 1
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.