Solved

Excel Application in VB

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now