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

  • SEARCH
  • Home
  • 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 8678891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:50:11+00:00 2026-06-12T20:50:11+00:00

I am getting this error: This command requires at least two rows of source

  • 0

I am getting this error:

This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following:

- If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again.
- I you're creating a PivotTable, type a cell reference or select a range that includes at least two rows of data

intermittently on this line of code:

xlWorkBook.RefreshAll();

There are two worksheets. One has a pivot table and one has raw data. Sometimes there is only one row of data. For multiple rows of data the line of code above always works; however, for only 1 row of data, the code above sometimes works, and sometimes I get the error message above.

In addition to this, the worksheet containing the pivot table is not refreshed; however, if I re-open the file, it also does not refresh, unless I explicitly refresh it manually.

What is going on here? Why am I getting this error only sometimes?

Thank you so much for your guidance.

if at all helpful, i am including the entire method:

private void SortandCreateFile(string column, string email, string emailStartPos) {
    string replacetext = "";

    try {
        var valueRange = xlWorkSheet.get_Range(column + emailStartPos, column + range.Rows.Count.ToString());
        var deleteRange = valueRange;
        xlApp.Visible = false;
        int startpos = 0;
        int endPos=0;
        bool foundStart = false;

        Excel.Range rng = xlWorkSheet.get_Range(column + "1", column + range.Rows.Count.ToString());

        string tempstring = "d";
        int INTemailStartPos = Convert.ToInt16(emailStartPos);

        for (int rCnt = INTemailStartPos; rCnt <= rng.Count; rCnt++) {
            Excel.Range cell = (Excel.Range)rng[rCnt, 1];

            try {
                if (cell.Value2 != null)
                    tempstring = cell.Value2.ToString();
                else {
                    startpos = rCnt;
                    releaseObject(cell);  /////////
                    break;
                }
            }
                catch (Exception ee)
            {
            MessageBox.Show(ee.ToString());
        }
        //grab the text from column link texdtbox
        Excel.Range rngLinkColumn;
        Excel.Range replacetextcell=null;

        if (FormControls.ColumnLink.Length > 0) {
            rngLinkColumn = xlWorkSheet.get_Range(FormControls.ColumnLink + "1", FormControls.ColumnLink + range.Rows.Count.ToString());
            replacetextcell = (Excel.Range)rngLinkColumn[rCnt, 1];
        }    
        //locate email
        if (cell.Value2.ToString() == email ) {
            //we found the starting position of the email we want!
            //this will tell us which row of data to start from
            startpos = rCnt;

            if (FormControls.ColumnLink.Length > 0)
                replacetext = replacetextcell.Value2.ToString();
            releaseObject(cell);  /////////
            break;
        }
        releaseObject(cell);
    }
    int foundstartminusONE = startpos - 1;
    int rngcount = rng.Count + INTemailStartPos;

    //delete everything from the top UNTIL the row of the email address that we need
    if (startpos != INTemailStartPos) {
        deleteRange = xlWorkSheet.get_Range(column + INTemailStartPos.ToString() + ":" + "CF" + foundstartminusONE.ToString(), Type.Missing);
        deleteRange = deleteRange.EntireRow;
        deleteRange.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
    }

    for (int rCnt = INTemailStartPos; rCnt <= rng.Count; rCnt++) {
        Excel.Range cell = (Excel.Range)rng[rCnt, 1];

        try {
            if (cell.Value2 != null )
                tempstring = cell.Value2.ToString();
            else {
                endPos = rCnt - 1;
                releaseObject(cell);////////
                break;
            }
        }
        catch (Exception ee) {
            //MessageBox.Show(ee.ToString());
        }    
        //locate email
        if (cell.Value2.ToString() != email ) {
            //we found where the last email address is that we need
            //this is where the issue is occurring i think with the deleting the last row
            endPos = rCnt;
            releaseObject(cell);////////
            break;
        }
        releaseObject(cell);
    }

    //delete all the stuff AFTER the email address that we need
    if (endPos != 0) {
        deleteRange = xlWorkSheet.get_Range(column + endPos + ":" + "CF" + rngcount.ToString(), Type.Missing);
        deleteRange = deleteRange.EntireRow;
        deleteRange.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
    }

    //when the user opens the excel file, we want the focus to be here
    var rangehome = xlWorkSheet.get_Range(FormControls.FocusOn, FormControls.FocusOn);
    xlWorkSheet.Activate();
    rangehome.Select();

    string filename = xlWorkBook.Path + @"\" + email + ".xlsx";
    string fileSubstring = filename.Substring(0, filename.IndexOf(".xlsx"));
    string randomfileString = Guid.NewGuid().ToString("N").Substring(0, 10) + ".xlsx";
    string targetfilenameRename = fileSubstring + randomfileString;

    //((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[FormControls.WorksheetFocus]).Activate();
    //((Excel.Worksheet)Excel.Application.ActiveWorkbook.Sheets[1]).Activate();  

    Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(FormControls.WorksheetFocus);
    xlWorkSheetFocus.Activate();
    xlWorkBook.SaveAs(targetfilenameRename, Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
                false, false, Excel.XlSaveAsAccessMode.xlNoChange,
                Type.Missing, Type.Missing, Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);

    try {
        xlWorkBook.RefreshAll();
    }
    catch { }
        xlWorkBook.Save();
        string targetfile = xlWorkBook.Path + @"\" + FormControls.FileName + " - "
                    + email.Substring(0, email.IndexOf("@")) + ".xlsx";
        System.IO.File.Copy(targetfilenameRename, targetfile, true);

        string body = FormControls.eMailBody;
        body = body.Replace("%replacetext%", replacetext);
        //replace %replacetext% in body
        string targetfileSubstring = targetfile.Substring(0, targetfile.IndexOf(".xlsx"));
        string randomString = Guid.NewGuid().ToString("N").Substring(0, 10)+".xlsx";
        string targetfileRename = targetfileSubstring+randomString;

        while (true) {
            try {
                SendEmail(targetfile, email, FormControls.eMailSubject, body,FormControls.eMailFrom);                                  
            }
            catch (Exception ee) {
                MessageBox.Show(ee.ToString());
                continue;
            }

            // all is good
            break;
        }
        releaseObject(valueRange);
        releaseObject(deleteRange);
        File.Copy(targetfile, targetfileRename, true);
    }
    catch (Exception e) {
        MessageBox.Show(e.ToString());
    }
    finally {
        //DisposeMe();
        // Release all COM RCWs.
        // The "releaseObject" will just "do nothing" if null is passed,
        // so no need to check to find out which need to be released.
        // The "finally" is run in all cases, even if there was an exception
        // in the "try". 
        // Note: passing "by ref" so afterwords "xlWorkSheet" will
        // evaluate to null. See "releaseObject".
        releaseObject(range);
        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        // The Quit is done in the finally because we always
        // want to quit. It is no different than releasing RCWs.
        if (xlApp != null) {
            xlApp.Quit();
        }
        releaseObject(xlApp);
    }
}
  • 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-06-12T20:50:12+00:00Added an answer on June 12, 2026 at 8:50 pm

    The only way I could replicate this error with a pivot table was by attempting to create one off a range that didn’t have column headers, just like on the screenshot from Stephan1010’s answer.

    In the GetPivotData Excel function, pivot fields are referred to by their names (=GETPIVOTDATA("EmailAddress",$A$3)); thus, it makes sense to disallow a data source that wouldn’t have them.

    The solution would be to pivot over a ListObject instead of a Range – in Excel when you select, say, range $A$1:$C$1 and format as table (from the Ribbon), the table that results will span $A$1:$C$2; the contents of the first row becomes the column headers and the second row is a valid, empty record. Interesting to note that this happens (the 2-row span) regardless of whether or not you check the “My table has headers” checkbox (the data will be moved to the first row and the table will contain default “Column1”-“Column2”-“Column3” headers if the checkbox is cleared).

    In other words, a ListObject is always a valid data source for a pivot table, while a Range may not contain enough rows. Also if you don’t have column headers and you create a pivot table with range $A$1:$C$2, the record at $A$1:$C$1 will be used as column headers, which means that first record is lost.

    From the code you have supplied I would presume the pivot table is already present and connected to some [named?] range in a template workbook that contains the macro. Turning your range into a table might be as trivial as selecting format as table from the Ribbon. And then you could have code like this to remove all unnecessary rows while still keeping a valid data source for the pivot table:

        public void DeleteExtraTableRows(string emailAddress, Excel.ListObject table)
        {
            try
            {
                var rowIndex = 0;
                var wasDeleted = false;
                while (rowIndex <= table.ListRows.Count)
                {
                    if (!wasDeleted) rowIndex++;
                    var row = table.ListRows[rowIndex];
    
                    var range = (Excel.Range)row.Range.Cells[1, 1];
                    var value = range.Value2;
    
                    if (value != null && !string.Equals(emailAddress, value.ToString()))
                    {
                        row.Delete();
                        wasDeleted = true;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n\n" + e.StackTrace);
            }
        }
    

    There is also a possibility that the email is never found in the loop’s if (cell.Value2.ToString() == email ) condition, which would end up deleting all rows from your range – even if the only difference is an extra space at the end of the in-cell value. With the above code, even if all email addresses get deleted the data source remains a valid one for a pivot table that would be connected to it.

    EDIT:
    In Excel you turn a Range into a ListObject by selecting the range in question and clicking the Format as table Ribbon button, from the Home tab. Alternatively you can create one like this:

                var range = ((Excel.Range)(worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[3, 1]]));
                var table = worksheet.ListObjects.Add(SourceType: Excel.XlListObjectSourceType.xlSrcRange, Source: range,
                                          XlListObjectHasHeaders: Excel.XlYesNoGuess.xlYes);
                table.TableStyle = "TableStyleMedium3";
    

    In code, you can access all ListObjects on a worksheet using the ListObjects property:

            var worksheet = (Excel.Worksheet) Globals.ThisAddIn.Application.ActiveSheet;
            var tables = worksheet.ListObjects;
    

    Then, you can access a specific ListObject /table with several different ways:

            var myTable = tables[1];
            var myTable = tables.Item["Table1"];
            var myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");
    

    As rows are added from the table, the actual range it refers to will be expanded accordingly; use myTable.Range to access the range in question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this weird error while running cucumber test: ERROR Mongo::OperationFailure: Database command
I recently started getting this error while running gems or bundler. The only thing
I am running Rails 3.1.1 and getting this error when running this command. Obviously
Im getting this error: Error: Command failed: execvp(): Permission denied When I do a
Getting this error: 2009-09-03 12:44:02.307 xcodebuild[307:10b] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler
Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
Getting this error when try to add an item to my repositories/context: Collection has
Am getting this error message and matched my brackets and couldn't find anything wrong.
I´m getting this error while trying to commit to a svn repository: svn: MKACTIVITY
Keep getting this error after inserting a subdatasheet into a query and trying to

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.