I am using the below code to insert a recordset into Access from Excel, insert total rows at each change in column A, and apply greenbar formatting to the specified range of data. What I want to do is to insert sum calculations into the subtotal rows, but do not know how to dynamically sum each set of data at each change in customer (col A). Example code would be appreciated. Thanks.
Option Compare Database
Option Explicit
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oApp As New Excel.Application
Dim lRow As Long
Dim lastrow As Long
Public strCriteria As String
Public strStore_Type As String
Public varItem As Variant
Public Progress As Variant
Sub Export_Qry()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Progress = SysCmd(acSysCmdInitMeter, "Exporting Data to Excel...", 21)
Set db = CurrentDb
Set rs = db.OpenRecordset("qryTotal_Share_SKU-Final", dbOpenSnapshot)
Set oBook = oApp.Workbooks.Open("U:\Desktop\Total Share by SKU_Mail-Retail.xls")
Set oSheet = oBook.Worksheets(2)
'Add the data starting at cell A5
oSheet.Range("A5").CopyFromRecordset rs
Progress = SysCmd(acSysCmdInitMeter, "Exporting Data to Excel...", 21)
Progress = SysCmd(acSysCmdUpdateMeter, 21)
Progress = SysCmd(acSysCmdClearStatus)
Progress = SysCmd(acSysCmdRemoveMeter)
Call ApplyGreenBarToSelection
Call Add_Totals
oBook.Worksheets(2).Activate
oApp.DisplayAlerts = False
oBook. SaveAs "U:\Desktop\Total_Share_SKU.xls"
oApp.DisplayAlerts = True
MsgBox "Export Complete!"
oBook.Close
oApp.Quit
'Close Recordset and clear objects
rs.Close
Set oBook = Nothing
Set oSheet = Nothing
Set oApp = Nothing
End Sub
Sub Add_Totals()
Dim i As Integer
oSheet.Select
For lRow = oSheet.Cells(oSheet.Rows.Count, "A").End(xlUp).Row To 6 Step -1
If oSheet.Cells(lRow, "A") <> oSheet.Cells(lRow - 1, "A") Then
oSheet.Rows(lRow).EntireRow.Insert
oSheet.Range("A" & lRow - 1).Select
oApp.Selection.Copy
oSheet.Range("A" & lRow).Select
oApp.ActiveSheet.Paste
oSheet.Range("B" & lRow).Select
oApp.ActiveCell.FormulaR1C1 = "Totals"
oApp.ActiveCell.Font.FontStyle = "Bold"
oApp.ActiveCell.Font.ColorIndex = 2
oApp.ActiveCell.Interior.ColorIndex = 50
oApp.ActiveCell.Interior.Pattern = xlSolid
oApp.Application.CutCopyMode = False
End If
Next lRow
End Sub
Sub ApplyGreenBarToSelection()
Dim c As Range
Dim lRow
Dim LastColumn As Integer
Dim ColumnLetter As String
lastrow = oSheet.Cells(oSheet.Rows.Count, "B").End(xlUp).Row
With oApp.Worksheets(2)
On Error Resume Next
LastColumn = oSheet.Cells. Find("*", oSheet.Cells(1), xlFormulas, _
xlWhole, xlByColumns, xlPrevious).Column
If Err <> 0 Then LastColumn = 0
End With
For Each c In oSheet.Range("A5:" & ExcelCol(LastColumn) & lastrow)
If c.Offset(-1, 0).Interior.ColorIndex = xlNone Then
oSheet.Range("A" & c.Row & ":" & ExcelCol(LastColumn) & c.Row).Interior.ColorIndex = 35
End If
If c.Offset(-1, 0).Interior.ColorIndex = 35 Then
oSheet.Range("A" & c.Row & ":" & ExcelCol(LastColumn) & c.Row).Interior.ColorIndex = xlNone
End If
Next c
End Sub
Public Function ExcelCol(intColNum As Integer) As String
Dim iLastChar As Integer
Dim iFirstChar As Integer
iFirstChar = (intColNum - 1) \ 26
iLastChar = (intColNum - 1) Mod 26
If iFirstChar > 0 Then
ExcelCol = Chr(Asc("A") + iFirstChar - 1) & Chr(Asc("A") + iLastChar)
Else
ExcelCol = Chr(Asc("A") + iLastChar)
End If
End Function
Option Compare Database
Option Explicit
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oApp As New Excel.Application
Dim lRow As Long
Dim lastrow As Long
Public strCriteria As String
Public strStore_Type As String
Public varItem As Variant
Public Progress As Variant
Sub Export_Qry()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Progress = SysCmd(acSysCmdInitMeter, "Exporting Data to Excel...", 21)
Set db = CurrentDb
Set rs = db.OpenRecordset("qryTotal_Share_SKU-Final", dbOpenSnapshot)
Set oBook = oApp.Workbooks.Open("U:\Desktop\Total Share by SKU_Mail-Retail.xls")
Set oSheet = oBook.Worksheets(2)
'Add the data starting at cell A5
oSheet.Range("A5").CopyFromRecordset rs
Progress = SysCmd(acSysCmdInitMeter, "Exporting Data to Excel...", 21)
Progress = SysCmd(acSysCmdUpdateMeter, 21)
Progress = SysCmd(acSysCmdClearStatus)
Progress = SysCmd(acSysCmdRemoveMeter)
Call ApplyGreenBarToSelection
Call Add_Totals
oBook.Worksheets(2).Activate
oApp.DisplayAlerts = False
oBook. SaveAs "U:\Desktop\Total_Share_SKU.xls"
oApp.DisplayAlerts = True
MsgBox "Export Complete!"
oBook.Close
oApp.Quit
'Close Recordset and clear objects
rs.Close
Set oBook = Nothing
Set oSheet = Nothing
Set oApp = Nothing
End Sub
Sub Add_Totals()
Dim i As Integer
oSheet.Select
For lRow = oSheet.Cells(oSheet.Rows.Count, "A").End(xlUp).Row To 6 Step -1
If oSheet.Cells(lRow, "A") <> oSheet.Cells(lRow - 1, "A") Then
oSheet.Rows(lRow).EntireRow.Insert
oSheet.Range("A" & lRow - 1).Select
oApp.Selection.Copy
oSheet.Range("A" & lRow).Select
oApp.ActiveSheet.Paste
oSheet.Range("B" & lRow).Select
oApp.ActiveCell.FormulaR1C1 = "Totals"
oApp.ActiveCell.Font.FontStyle = "Bold"
oApp.ActiveCell.Font.ColorIndex = 2
oApp.ActiveCell.Interior.ColorIndex = 50
oApp.ActiveCell.Interior.Pattern = xlSolid
oApp.Application.CutCopyMode = False
End If
Next lRow
End Sub
Sub ApplyGreenBarToSelection()
Dim c As Range
Dim lRow
Dim LastColumn As Integer
Dim ColumnLetter As String
lastrow = oSheet.Cells(oSheet.Rows.Count, "B").End(xlUp).Row
With oApp.Worksheets(2)
On Error Resume Next
LastColumn = oSheet.Cells. Find("*", oSheet.Cells(1), xlFormulas, _
xlWhole, xlByColumns, xlPrevious).Column
If Err <> 0 Then LastColumn = 0
End With
For Each c In oSheet.Range("A5:" & ExcelCol(LastColumn) & lastrow)
If c.Offset(-1, 0).Interior.ColorIndex = xlNone Then
oSheet.Range("A" & c.Row & ":" & ExcelCol(LastColumn) & c.Row).Interior.ColorIndex = 35
End If
If c.Offset(-1, 0).Interior.ColorIndex = 35 Then
oSheet.Range("A" & c.Row & ":" & ExcelCol(LastColumn) & c.Row).Interior.ColorIndex = xlNone
End If
Next c
End Sub
Public Function ExcelCol(intColNum As Integer) As String
Dim iLastChar As Integer
Dim iFirstChar As Integer
iFirstChar = (intColNum - 1) \ 26
iLastChar = (intColNum - 1) Mod 26
If iFirstChar > 0 Then
ExcelCol = Chr(Asc("A") + iFirstChar - 1) & Chr(Asc("A") + iLastChar)
Else
ExcelCol = Chr(Asc("A") + iLastChar)
End If
End Function