Solved

Excel Application in VB

Posted on 2002-06-30
3
199 Views
Last Modified: 2010-05-02
hi, below is a Sub that works with Excel. Now, I don't have problem writing details into Excel file. I would like to do some calculation on Adjustment, CurrentCharges and TaxAmount after all details (like using sum function). How to do that?

Thanks.

Public Sub tryExcel()
    Dim i As Integer
    Dim strSQL As String
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
   
    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
   
    xlSheet.Cells(1, 1).Value = "Invoice Summary Report"
    xlSheet.Cells(2, 1).Value = "Date: {09/01/2002}"
    xlSheet.Cells(4, 1).Value = "Account No"
    xlSheet.Cells(4, 2).Value = "Invoice No"
    xlSheet.Cells(4, 3).Value = "Adjustment (RM)"
    xlSheet.Cells(4, 4).Value = "Current Charges (RM)"
    xlSheet.Cells(4, 5).Value = "Govt. Tax (RM)"
    xlSheet.Cells(4, 6).Value = "Total Amount (RM)"
   
    strSQL = "Select * From aInvoice_SummaryReport Order By InvoiceNo Asc"
   
    Call OpenRS(Rs, strSQL)
    If Not (Rs.BOF Or Rs.EOF) Then
        nTotalRec = Rs.RecordCount
        For i = 0 To nTotalRec - 1 Step 1
            xlSheet.Cells(i + 5, 1).Value = Rs.Fields("AccNo")
            xlSheet.Cells(i + 5, 2).Value = Rs.Fields("InvoiceNo")
            xlSheet.Cells(i + 5, 3).Value = Rs.Fields("Adjustment")
            xlSheet.Cells(i + 5, 4).Value = Rs.Fields("CurrentCharges")
            xlSheet.Cells(i + 5, 5).Value = Rs.Fields("TaxAmount")
            xlSheet.Cells(i + 5, 6).Value = Rs.Fields("Adjustment") + _
                                        Rs.Fields("CurrentCharges") + _
                                        Rs.Fields("TaxAmount")
            Rs.MoveNext
        Next i
    End If
    Call CloseRS(Rs)
    xlSheet.Cells(nTotalRec + 5 + 1, 2).Value = "Grand Total"
   
    'xlSheet.Cells(nTotalRec + 5 + 1, 6).Value
    xlSheet.SaveAs CurrentDirectory & "\Test.xls"
    xlSheet.Application.Quit
    Set xlBook = Nothing
    Set xlApp = Nothing
End Sub
0
Comment
Question by:icecool
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7121281
I assume that you want the commented line ('xlSheet.Cells(nTotalRec + 5 + 1, 6).Value)  to have a sum of the previous rows.

xlSheet.Cells(nTotalRec + 5 + 1, 6).FormulaR1C1 = "=SUM(R[-" & CStr(nTotalRec - 5) & "]C:R[-1]C)"

This will use the standard Excel Sum() function and set the range to be the records you have just inserted to the spreadsheet.
0
 

Author Comment

by:icecool
ID: 7125992
hi, TimCottee,

I tried that but an error occured Run-Time error 1004, Application-defined or object-defined error.

What is the problem?
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 7126323
I didn't really test it out, just modified an example I have previously used. You could try a variant of this that doesn't use the R1C1 method:

xlSheet.Cells(nTotalRec + 6,6).Formula = "=Sum(F5:F" & CStr(nTotalRec)

This is just another way of doing it (I think is often easier to use than the R1C1 notation as it is more familiar).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question