This command requires at least two rows of source data

前端 未结 3 1625
长发绾君心
长发绾君心 2021-01-19 15:03

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         


        
相关标签:
3条回答
  • 2021-01-19 15:43

    The obvious answer seems to be that sometimes you have one row of data as the source for your pivot table and sometimes you don't - even when you think you still do. I have not been able to create a pivot table(or change the source of a pivot table) to one row of data:

    PivotTableErrorExplanation

    but if you are able to somehow figure out a way to do this then you have found your answer. There is no reason you can't have one row of data as your source just from a practical/theoretical perspective, but it looks like excel tries to prevent that from happening(maybe because the code assumes two rows). So if you do find a way, then it is probably a bug. Good Luck.

    0 讨论(0)
  • 2021-01-19 15:44

    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 讨论(0)
  • 2021-01-19 16:04

    i suppose this situation occurs because of the pivot tables you got. cause refresh all will trigger pivot table's refresh command too. look at the code below. It may give you an idea about it. Its not about 1 row im sure. i checked it everthing works just fine its most posibly caused by pivot tables.

    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("some.xlsx");
    // For each worksheet we got
    foreach (Microsoft.Office.Interop.Excel.Worksheet worksheet in xlWorkbook.Sheets) 
    {   // and each pivot table in each worksheet
        foreach (Microsoft.Office.Interop.Excel.PivotTable pivot in worksheet.PivotTables())
        {   // disable BackgroundQuery
            pivot.PivotTableWizard(BackgroundQuery: false);
        }
    }
    // try to refresh all sheet
    try { xlWorkbook.RefreshAll(); } catch { }
    // then save
    xlWorkbook.Save();
    
    0 讨论(0)
提交回复
热议问题