Issues with EXCEL subtotal using vb.net

Rex Haverty 21 Reputation points
2020-11-12T17:21:59.217+00:00

I am getting random Grand Totals when I use vb.Net to sub-total the spreadsheet: The code I am using is: enter code herePublic Function SubTotalBudgetSummary(ixlsxWorkSheet As Excel.Worksheet) As Boolean

    Dim lexcelRange As Object = Nothing  

    '<PageBreak>  
    Try  

        lexcelRange = ("A1:K") & ixlsxWorkSheet.UsedRange.Rows.Count.ToString  

        ' ----  Sub Total Project Number  
        If InsertSubTotal(ixlsxWorkSheet:=ixlsxWorkSheet,  
                          iExcelRange:=lexcelRange,  
                          iintGroupBy:=1,  
                          ibolReplaceTotals:=vbTrue,  
                          ibolPageBreak:=vbTrue) = vbFalse Then  
            Throw New Exception(mstrClsTitle & ".ExportBudgetDataGridToExcel" &  
                                "Failed to Insert Project No SubTotals")  
        End If  

        ' ----  Sub Total Cost Center  
        If InsertSubTotal(ixlsxWorkSheet:=ixlsxWorkSheet,  
                          iExcelRange:=lexcelRange,  
                          iintGroupBy:=2,  
                          ibolReplaceTotals:=vbFalse,  
                          ibolPageBreak:=vbTrue) = vbFalse Then  
            Throw New Exception(mstrClsTitle & ".ExportBudgetDataGridToExcel" &  
                                "Failed to Insert Cost Center SubTotals")  
        End If  

        ' ----  Sub Total Labor Class  
        If InsertSubTotal(ixlsxWorkSheet:=ixlsxWorkSheet,  
                          iExcelRange:=lexcelRange,  
                          iintGroupBy:=3,  
                          ibolReplaceTotals:=vbFalse,  
                          ibolPageBreak:=vbTrue) = vbFalse Then  
            Throw New Exception(mstrClsTitle & ".ExportBudgetDataGridToExcel" &  
                                "Failed to Insert Labor Class SubTotals")  
        End If  

        ' ----  Sub Total Account Number  
        If InsertSubTotal(ixlsxWorkSheet:=ixlsxWorkSheet,  
                          iExcelRange:=lexcelRange,  
                          iintGroupBy:=4,  
                          ibolReplaceTotals:=vbFalse,  
                          ibolPageBreak:=vbTrue) = vbFalse Then  
            Throw New Exception(mstrClsTitle & ".ExportBudgetDataGridToExcel" &  
                                "Failed to Insert Account Number SubTotals")  
        End If  
        Return vbTrue  

        '<PageBreak>  
    Catch ex As Exception  
        modProcessMessage.ProcessMessage(istrMessage:=mstrClsTitle & ".SubTotalBudgetSummary " &  
                                                      "An error occurred while inserting the " &  
                                                      "totals in the Excel Worksheet",  
                                         iintIndent:=6,  
                                         istrErrNumber:=Err.Number.ToString,  
                                         istrErrDescription:=Err.Description)  
        Return vbFalse  
    Finally  
        modReleaseObject.ReleaseObject(lexcelRange)  
    End Try  
End Function  

Private Function InsertSubTotal(ByRef ixlsxWorkSheet As Excel.Worksheet,
ByVal iExcelRange As Object,
ByVal iintGroupBy As Integer,
ByVal ibolReplaceTotals As Boolean,
ByVal ibolPageBreak As Boolean) As Boolean

    Try  

        '   Macro Code  
        With ixlsxWorkSheet  
            .Range(iExcelRange).Subtotal(GroupBy:=iintGroupBy,  
                                         Function:=Excel.XlConsolidationFunction.xlSum,  
                                         TotalList:=New Int32() {6, 7, 8, 9, 10, 11},  
                                         Replace:=ibolReplaceTotals,  
                                         PageBreaks:=ibolPageBreak,  
                                         SummaryBelowData:=vbTrue)  
        End With  

        'With ixlsxWorkSheet  
        '    .Range(iExcelRange).Subtotal(GroupBy:=iintGroupBy,  
        '                                 Function:=Excel.XlConsolidationFunction.xlSum,  
        '                                 TotalList:=New Int32() {6, 7, 8, 9, 10, 11},  
        '                                 Replace:=ibolReplaceTotals,  
        '                                 PageBreaks:=ibolPageBreak,  
        '                                 SummaryBelowData:=vbTrue)  
        'End With  
        Return vbTrue  

    Catch ex As Exception  
        modProcessMessage.ProcessMessage(istrMessage:=mstrClsTitle & ".SubTotal_ProjNo " &  
                                                      "An error occurred while Subtotaling " &  
                                                      "Project Number",  
                                         iintIndent:=6,  
                                         istrErrNumber:=Err.Number.ToString,  
                                         istrErrDescription:=Err.Description)  
        Return vbFalse  
    End Try  
End Function  

39482-excel-sample.png

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2020-11-12T18:46:40.793+00:00

    Excel development is not currently supported here on QnA. They're actively answering questions in dedicated forums here.
    https://social.msdn.microsoft.com/Forums/Lync/en-US/home?forum=exceldev
    https://social.msdn.microsoft.com/Forums/Lync/en-US/home?forum=vsto
    https://social.msdn.microsoft.com/Forums/Lync/en-US/home?forum=vbgeneral

    --please don't forget to Accept as answer if the reply is helpful--

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.