Question : Adjust Excel document to where it totals a column using Microsoft Access VBA?

Please note the following code where I give a path of a document and export a query to it.  In column AE of Excel, I want to total the column results and place it not on the next row but rather skip a row and put the totals on the next line item.  The problem for me is determining where that exported query to Excel will end because at various times the last row with its complete number will shrink or grow.

So please give me the VBA coding to let me make a valid totals of a column one row after it ends.

Thank you in advance!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
Public Sub Excel_This(sQuery As String, sExcelFileName As String)
On Error GoTo Err_This

'  Dim xlapp As excel.Application
'  Dim wb As excel.workbook
'  Dim ws As excel.worksheet

  Dim xlapp As Object
  Dim xlBook As Object
  Dim xlSheet As Object
  Dim rs As DAO.Recordset
  
  Set xlapp = CreateObject("Excel.Application")
  Set xlBook = xlapp.Workbooks.Open(sExcelFileName)
  Set xlSheet = xlBook.Worksheets(1)

  Set rs = CurrentDb.OpenRecordset(sQuery)
  xlSheet.range("A2").copyfromrecordset rs
  
  xlapp.Visible = True
  
'
'  Set xlSheet = Nothing
'  Set xlBook = Nothing
'  Set xlapp = Nothing
  
Exit_This:
  Exit Sub
  
Err_This:
  Call Error_Action(Err, Err.description, "modECdatabases @ Excel_This", Erl())
  Err = 0
  Resume Exit_This

End Sub

Answer : Adjust Excel document to where it totals a column using Microsoft Access VBA?

Based on that result, there must be something in AE65535, perhaps a stray space.

Try this:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
Public Sub Excel_This(sQuery As String, sExcelFileName As String)
On Error GoTo Err_This

'  Dim xlapp As excel.Application
'  Dim wb As excel.workbook
'  Dim ws As excel.worksheet

  Dim xlapp As Object
  Dim xlBook As Object
  Dim xlSheet As Object
  Dim rs As DAO.Recordset
  Dim LastR As Long
  
  Set xlapp = CreateObject("Excel.Application")
  Set xlBook = xlapp.Workbooks.Open(sExcelFileName)
  Set xlSheet = xlBook.Worksheets(1)

  Set rs = CurrentDb.OpenRecordset(sQuery)
  With xlSheet
    .Range("2:" & .Rows.Count).Delete
    .Range("A2").copyfromrecordset rs
    LastR = .Cells(.Rows.Count, "ae").End(-4162).Row  ' xlUp = -4162
    .Range("ae" & LastR + 2) = "=SUM(AE2:AE" & LastR & ")"
  End With
  
  xlapp.Visible = True
  
'
'  Set xlSheet = Nothing
'  Set xlBook = Nothing
'  Set xlapp = Nothing
  
Exit_This:
  Exit Sub
  
Err_This:
  Call Error_Action(Err, Err.description, "modECdatabases @ Excel_This", Erl())
  Err = 0
  Resume Exit_This

End Sub
Random Solutions  
 
programming4us programming4us