mark curtis
Registered User.
- Local time
- Today, 19:13
- Joined
- Oct 9, 2000
- Messages
- 457
Dear all,
I can transfer records to Excel from Access ok but I am having real problems getting my head round the following:
When I transfer all my required records I now wish to insert a totals row, which should start at the row after the last with data and commence from the third column in?? Basically I need to know how to get around my range???
I also want to loop through all the text entries in column 3 and change the colour based on the text entry? Also i need the loop to only loop through the data I have transferred from Access?
I use the code below to export but need to build on this.
Any help as always much appreciated!
Dim DB As DAO.Database, Rs As DAO.Recordset
Dim i As Integer, j As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim Workbook As Object
Dim xlApp As Object
Dim Sheet As Object
Dim CurCell As Object
On Error GoTo Err_Command695_Click
Set DB = DBEngine.Workspaces(0).Databases(0)
'RsSql = "SELECT * FROM [Order Details] WHERE [OrderId]< 10249;"
Set Rs = DB.OpenRecordset("RORY", dbOpenDynaset)
'If recordset has no data then message and cancel
If Rs.RecordCount = 0 Then
DisplayMessage "There are no Products to export?"
Exit Sub
End If
Set xlApp = CreateObject("Excel.Application")
Set Workbook = xlApp.Workbooks.Add
Set Sheet = xlApp.ActiveWorkbook.Sheets(1)
j = 1
'Open Excel, maximise it and disable alerts.
With xlApp
.Visible = True
.WindowState = xlMaximized
.DisplayAlerts = False
.Interactive = True
' Loop through the Microsoft Access field names and create
' the Microsoft Excel labels.
For i = 0 To Rs.Fields.Count - 1
CurrentValue = Rs.Fields(i).Name
Sheet.Cells(j, i + 1).Value = CurrentValue
Next i
j = 2
' Loop through the Microsoft Access records and copy the records
' to the Microsoft Excel spreadsheet.
Do Until Rs.EOF
For i = 0 To Rs.Fields.Count - 1
CurrentField = Rs(i)
Sheet.Cells(j, i + 1).Value = CurrentField
Next i
Rs.MoveNext
j = j + 1
Loop
' Print the Microsoft Excel spreadsheet and refit all columns
.Cells.Select
.Selection.ColumnWidth = 1
.Cells.EntireColumn.AutoFit
.Cells(1, 1).Select
'Prompt user to save file
Dim Fname
Do
Fname = xlApp.GetSaveAsFilename
Loop Until Fname <> False
Workbook.SaveAs FileName:=Fname
.Range("A1:GP1").Select
.Selection.Font.Bold = True
FORMAT SHEET CODE REQUIRED
Set xlApp = Nothing
I can transfer records to Excel from Access ok but I am having real problems getting my head round the following:
When I transfer all my required records I now wish to insert a totals row, which should start at the row after the last with data and commence from the third column in?? Basically I need to know how to get around my range???
I also want to loop through all the text entries in column 3 and change the colour based on the text entry? Also i need the loop to only loop through the data I have transferred from Access?
I use the code below to export but need to build on this.
Any help as always much appreciated!
Dim DB As DAO.Database, Rs As DAO.Recordset
Dim i As Integer, j As Integer
Dim RsSql As String
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim Workbook As Object
Dim xlApp As Object
Dim Sheet As Object
Dim CurCell As Object
On Error GoTo Err_Command695_Click
Set DB = DBEngine.Workspaces(0).Databases(0)
'RsSql = "SELECT * FROM [Order Details] WHERE [OrderId]< 10249;"
Set Rs = DB.OpenRecordset("RORY", dbOpenDynaset)
'If recordset has no data then message and cancel
If Rs.RecordCount = 0 Then
DisplayMessage "There are no Products to export?"
Exit Sub
End If
Set xlApp = CreateObject("Excel.Application")
Set Workbook = xlApp.Workbooks.Add
Set Sheet = xlApp.ActiveWorkbook.Sheets(1)
j = 1
'Open Excel, maximise it and disable alerts.
With xlApp
.Visible = True
.WindowState = xlMaximized
.DisplayAlerts = False
.Interactive = True
' Loop through the Microsoft Access field names and create
' the Microsoft Excel labels.
For i = 0 To Rs.Fields.Count - 1
CurrentValue = Rs.Fields(i).Name
Sheet.Cells(j, i + 1).Value = CurrentValue
Next i
j = 2
' Loop through the Microsoft Access records and copy the records
' to the Microsoft Excel spreadsheet.
Do Until Rs.EOF
For i = 0 To Rs.Fields.Count - 1
CurrentField = Rs(i)
Sheet.Cells(j, i + 1).Value = CurrentField
Next i
Rs.MoveNext
j = j + 1
Loop
' Print the Microsoft Excel spreadsheet and refit all columns
.Cells.Select
.Selection.ColumnWidth = 1
.Cells.EntireColumn.AutoFit
.Cells(1, 1).Select
'Prompt user to save file
Dim Fname
Do
Fname = xlApp.GetSaveAsFilename
Loop Until Fname <> False
Workbook.SaveAs FileName:=Fname
.Range("A1:GP1").Select
.Selection.Font.Bold = True
FORMAT SHEET CODE REQUIRED
Set xlApp = Nothing