Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 239441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:35:08+00:00 2026-05-11T20:35:08+00:00

I’m nedding to implement excel formula autofill in C#. Let’s suppose this formula is

  • 0

I’m nedding to implement excel formula autofill in C#.

Let’s suppose this formula is located at B100:

=SUM($B$99:B99)

I want to make this formula different at C100:

=SUM($B$99:C99)

This formula is only an example. Some real examples are:

=(SUM($B${0}:B{0})/SUM({1}!$B${0}:{1}!B{0}) -1)

=SUM(B{0}:B{1})

=B{0} + B{1}

=C{0}+ B{1}

=$B${0}+ AC{1}

(consider {0} and {1} are, in fact, numbers)

What I need to do, generically, is to pick these column names and “increment” them. Column names surrounded by $ in formulas should not be updated.

How to identify these fields with regex?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:35:08+00:00Added an answer on May 11, 2026 at 8:35 pm

    Here’s a regex solution that solely deals with formulas. I’ll leave the Excel stuff to you. Provided you have a collection of strings representing your formulas, you can run them through this to increment your column names.

    Some comments:

    • **Test this thoroughly!** Perhaps do a sheet manually and compare your efforts with the generated results.
    • This shouldn’t accidentally alter function names that fit the cell naming pattern. If you know your formulas have Excel function names that include numbers, keep an eye out for them and – again – **verify results**.
    • The regex does not verify what you’re feeding it is a formula – I take it you’re only using formulas. In other words, I didn’t make it check that the string begins with an “=” sign. If you plan to feed non-formulas through this for other cell values, then add a check where IsMatch is used in the if branch using formula.StartsWith(“=”). To understand what I’m referring to, add an additional test string to my sample, such as “Check out T4 generation” – if no StartsWith(“=”) check is made, that will match and T4 will become U4.

    The regex pattern was actually the easy part. It will just match any letter-number sequence, and ignores $A$1 and $A1 types of cells. The tricky part was the logic to increment the column. I’ve added comments to clarify that bit so grab some coffee and read it over 🙂

    I’m sure this could be enhanced but this is what I had time for.

    using System.Text.RegularExpressions;
    
    static void Main(string[] args)
    {
        string[] formulas = { "Z1", "ZZ1", "AZ1", "AZB1", "BZZ2",
                            "=SUM($B$99:B99)","=SUM($F99:F99)", "=(SUM($B$0:B0)/SUM(1!$B$11:22!B33) -1)",
                            "=SUM(X80:Z1)", "=A0 + B1 - C2 + Z5", "=C0+ B1",
                            "=$B$0+ AC1", "=AA12-ZZ34 + AZ1 - BZ2 - BX3 + BZX4",
                            "=SUMX2MY2(A2:A8,B2:B8)",   // ensure function SUMX2MY2 isn't mistakenly incremented
                            "=$B$40 + 50 - 20"          // no match
                            //,"Check out T4 generation!"  // not a formula but it'll still increment T4, use formula.StartsWith("=")
                            };
    
        // use this if you don't want to include regex comments
        //Regex rxCell = new Regex(@"(?<![$])\b(?<col>[A-Z]+)(?<row>\d+)\b");
    
        // regex comments in this style requires RegexOptions.IgnorePatternWhitespace
        string rxCellPattern = @"(?<![$])       # match if prefix is absent: $ symbol (prevents matching $A1 type of cells)
                                                # (if all you have is $A$1 type of references, and not $A1 types, this negative look-behind isn't needed)
                                \b              # word boundary (prevents matching Excel functions with a similar pattern to a cell)
                                (?<col>[A-Z]+)  # named capture group, match uppercase letter at least once
                                                # (change to [A-Za-z] if you have lowercase cells)
                                (?<row>\d+)     # named capture group, match a number at least once
                                \b              # word boundary
                                ";
        Regex rxCell = new Regex(rxCellPattern, RegexOptions.IgnorePatternWhitespace);
    
        foreach (string formula in formulas)
        {
            if (rxCell.IsMatch(formula))
            {
                Console.WriteLine("Formula: {0}", formula);
                foreach (Match cell in rxCell.Matches(formula))
                    Console.WriteLine("Cell: {0}, Col: {1}", cell.Value, cell.Groups["col"].Value);
    
                // the magic happens here
                string newFormula = rxCell.Replace(formula, IncrementColumn);
                Console.WriteLine("Modified: {0}", newFormula);
            }
            else
            {
                Console.WriteLine("Not a match: {0}", formula);
            }
            Console.WriteLine();
        }
    }
    
    
    private static string IncrementColumn(Match m)
    {
        string col = m.Groups["col"].Value;
        char c;
    
        // single character column name (ie. A1)
        if (col.Length == 1)
        {
            c = Convert.ToChar(col);
            if (c == 'Z')
            {
                // roll over
                col = "AA";
            }
            else
            {
                // advance to next char
                c = (char)((int)c + 1);
                col = c.ToString();
            }
        }
        else
        {
            // multi-character column name (ie. AB1)
            // in this case work backwards to do some column name "arithmetic"
            c = Convert.ToChar(col.Substring(col.Length - 1, 1));   // grab last letter of col
    
            if (c == 'Z')
            {
                string temp = "";
                for (int i = col.Length - 1; i >= 0; i--)
                {
                    // roll over should occur
                    if (col[i] == 'Z')
                    {
                        // prepend AA if current char is not the last char in column and its next neighbor was also a Z
                        // ie. column BZZ: if current char is 1st Z, it's neighbor Z (2nd Z) just got incremented, so 1st Z becomes AA
                        if (i != col.Length - 1 && col[i + 1] == 'Z')
                        {
                            temp = "AA" + temp;
                        }
                        else
                        {
                            // last char in column is Z, becomes A (this will happen first, before the above if branch ever happens)
                            temp = "A" + temp;
                        }
                    }
                    else
                    {
                        temp = ((char)((int)col[i] + 1)).ToString() + temp;
                    }
                }
                col = temp;
            }
            else
            {
                // advance char
                c = (char)((int)c + 1);
                // chop off final char in original column, append advanced char
                col = col.Remove(col.Length - 1) + c.ToString();
            }
        }
    
        // updated column and original row (from regex match)
        return col + m.Groups["row"].Value;
    }
    

    The results should look like this (I removed the cell breakdown for brevity):

    Formula: Z1
    Modified: AA1
    
    Formula: ZZ1
    Modified: AAA1
    
    Formula: AZ1
    Modified: BA1
    
    Formula: AZB1
    Modified: AZC1
    
    Formula: BZZ2
    Modified: CAAA2
    
    Formula: =SUM($B$99:B99)
    Modified: =SUM($B$99:C99)
    
    Formula: =SUM($F99:F99)
    Modified: =SUM($F99:G99)
    
    Formula: =(SUM($B$0:B0)/SUM(1!$B$11:22!B33) -1)
    Modified: =(SUM($B$0:C0)/SUM(1!$B$11:22!C33) -1)
    
    Formula: =SUM(X80:Z1)
    Modified: =SUM(Y80:AA1)
    
    Formula: =A0 + B1 - C2 + Z5
    Modified: =B0 + C1 - D2 + AA5
    
    Formula: =C0+ B1
    Modified: =D0+ C1
    
    Formula: =$B$0+ AC1
    Modified: =$B$0+ AD1
    
    Formula: =AA12-ZZ34 + AZ1 - BZ2 - BX3 + BZX4
    Modified: =AB12-AAA34 + BA1 - CA2 - BY3 + BZY4
    
    Formula: =SUMX2MY2(A2:A8,B2:B8)
    Modified: =SUMX2MY2(B2:B8,C2:C8)
    
    Not a match: =$B$40 + 50 - 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.