gillianleec
Technical User
I have a series of queries that I transfer to a workbook and then format using VBA. My problem is the section of code below that only works correctly on the first spreadsheet.
Dim xlApp As Excel.Application
Dim objExcel As Object
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set db = CurrentDb
Set rs = db.OpenRecordset("Select DISTINCT tabname, query, columns, sum, deletecolumn, sumcolumn, cletter from tblExcelTabDefs where Type ='" & Me.cboPType & "';")
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
strTitle = rs.Fields(0)
strQry = rs.Fields(1)
strcolumn = rs.Fields(2)
strSum = rs.Fields(3)
strDColumn = rs.Fields(4)
strSColumn = rs.Fields(5)
strLet = rs.Fields(6)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel3, strQry, "C:\Documents and Settings\glcoykendall\My Documents\" & Me.cboService.Value & strReport, True
Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:\Documents and Settings\glcoykendall\My Documents\" & Me.cboService.Value & strReport)
Set xlSheet = xlBook.Sheets(strQry)
If objExcel Is Nothing Then
Set objExcel = New Excel.Application
objExcel.EnableEvents = False 'disable Excel save messages
End If
Dim aArea As Object
Dim i As Integer
Set aArea = xlSheet.Range("A:A")
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Check to see if this is a sheet that needs to be summed and then perform insert and sum calculation
'****************************************************************************************************
If IsNull(strSColumn) = False Then
'*****Insert Row between Unique Column 1 Values*****
For i = xlSheet.Range("A5000").End(xlUp).Row To 3 Step -1
If xlSheet.Range("A" & i) <> xlSheet.Range("A" & i - 1) Then
xlSheet.Range("A" & i).EntireRow.Insert shift:=xlDown
End If
Next i
'*****Perform Sum of each section between inserted rows*****
i = 0
For Each aArea In xlSheet.UsedRange.SpecialCells(xlCellTypeConstants).Areas
i = i + 1
If i <> 1 Then
With xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)
.Value = WorksheetFunction.Sum(xlSheet.Range(xlSheet.Cells(aArea.Row, strSColumn), xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)))
With .Font
.ColorIndex = 5
.Bold = True
End With
End With
End If
Next aArea
'End With
End If
xlBook.Save
With xlBook
.Save
End With
'Set xlSheet = Nothing
Set aArea = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
The section that is not working is this part: With xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)
.Value = WorksheetFunction.Sum(xlSheet.Range(xlSheet.Cells(aArea.Row, strSColumn), xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)))
It works for the first sheet but subsequent sheets only have a zero where the sum should be.
Thanks,
Gillian
Dim xlApp As Excel.Application
Dim objExcel As Object
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set db = CurrentDb
Set rs = db.OpenRecordset("Select DISTINCT tabname, query, columns, sum, deletecolumn, sumcolumn, cletter from tblExcelTabDefs where Type ='" & Me.cboPType & "';")
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
strTitle = rs.Fields(0)
strQry = rs.Fields(1)
strcolumn = rs.Fields(2)
strSum = rs.Fields(3)
strDColumn = rs.Fields(4)
strSColumn = rs.Fields(5)
strLet = rs.Fields(6)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel3, strQry, "C:\Documents and Settings\glcoykendall\My Documents\" & Me.cboService.Value & strReport, True
Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:\Documents and Settings\glcoykendall\My Documents\" & Me.cboService.Value & strReport)
Set xlSheet = xlBook.Sheets(strQry)
If objExcel Is Nothing Then
Set objExcel = New Excel.Application
objExcel.EnableEvents = False 'disable Excel save messages
End If
Dim aArea As Object
Dim i As Integer
Set aArea = xlSheet.Range("A:A")
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Check to see if this is a sheet that needs to be summed and then perform insert and sum calculation
'****************************************************************************************************
If IsNull(strSColumn) = False Then
'*****Insert Row between Unique Column 1 Values*****
For i = xlSheet.Range("A5000").End(xlUp).Row To 3 Step -1
If xlSheet.Range("A" & i) <> xlSheet.Range("A" & i - 1) Then
xlSheet.Range("A" & i).EntireRow.Insert shift:=xlDown
End If
Next i
'*****Perform Sum of each section between inserted rows*****
i = 0
For Each aArea In xlSheet.UsedRange.SpecialCells(xlCellTypeConstants).Areas
i = i + 1
If i <> 1 Then
With xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)
.Value = WorksheetFunction.Sum(xlSheet.Range(xlSheet.Cells(aArea.Row, strSColumn), xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)))
With .Font
.ColorIndex = 5
.Bold = True
End With
End With
End If
Next aArea
'End With
End If
xlBook.Save
With xlBook
.Save
End With
'Set xlSheet = Nothing
Set aArea = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
The section that is not working is this part: With xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)
.Value = WorksheetFunction.Sum(xlSheet.Range(xlSheet.Cells(aArea.Row, strSColumn), xlSheet.Cells(aArea.Row + aArea.Rows.Count, strSColumn)))
It works for the first sheet but subsequent sheets only have a zero where the sum should be.
Thanks,
Gillian