Solved

Excel Application in VB

Posted on 2002-06-30
3
198 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

821 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