Hi all,
I posted this in the VBA forum yesterday and got some good help there, but the problem is not fixed. I have an Access query which, on a button click, is being exported to Excel. However I wanted the Excel sheet to be formatted in a certain way. I've got the code so that it works OK once, but if you try to run it again, I get one of a number of errors, but usually 1004: Method 'cells' of object '_Global' failed.
I've looked really hard at my code and I think it's fine - well it must be, to work once, but there is something wierd going on where it will only work again if I shut the db down and start it up again, or compact it.
Has anyone got any general ideas on what might be happening? I'm posting the code below. I don't think it's a specific Excel VBA coding issue, which is why I'm trying in this forum too....
Option Compare Database
Option Explicit
Dim xlProjectCosts As Excel.Application
Public Sub CreateLifeCycleReport(ProjectName As String, QueryName As String)
Dim cnCurrent As ADODB.Connection
Dim rsMatrix As ADODB.Recordset
Dim R As Long
Dim C As Integer
Dim i As Integer
Dim xlProjectCosts As Excel.Application
'Dim mWB As Excel.Workbook
On Error GoTo ErrHandler
Set cnCurrent = CurrentProject.Connection
Set rsMatrix = New ADODB.Recordset
rsMatrix.Open "SELECT * FROM " & QueryName, cnCurrent, adOpenDynamic, adLockPessimistic
Set xlProjectCosts = New Excel.Application
xlProjectCosts.Visible = True
xlProjectCosts.Workbooks.Add
'xlProjectCosts.ActiveWindow.DisplayGridlines = False
'Set mWB = ActiveWorkbook
'Enter details of project, autofit cells and put into bold type
With xlProjectCosts
With .ActiveWorkbook
.ActiveSheet.Cells(1, 1).Value = "Prepared:"
.ActiveSheet.Cells(1, 2).Value = Now()
.ActiveSheet.Columns(2).AutoFit
.ActiveSheet.Cells(3, 1).Value = "Project:"
.ActiveSheet.Cells(3, 2).Value = ProjectName
.ActiveSheet.Columns(2).AutoFit
.ActiveSheet.Range("A1:B3"
.Font.Bold = True
R = 5
'Enter Field names into row 5 (this is a crosstab query)
For i = 1 To rsMatrix.Fields.Count
.ActiveSheet.Cells(R, i + 2).Value = rsMatrix.Fields(i).Name
.ActiveSheet.Cells(R, i + 2).Font.Bold = True
.ActiveSheet.Columns(i + 2).ColumnWidth = 11.14
Next
'I want a bit of space between headings and the rest of the data so set current row to 7
R = 7
C = rsMatrix.Fields.Count + 2
'copy data from recordset - the queries I'm putting in all work fine
.ActiveSheet.Range("B7"
.CopyFromRecordset rsMatrix
'the first column has text in so I'd like to leave it out of the formatting
R = .ActiveSheet.Range("C65536"
.End(xlUp).Row
'select the range with numbers in and format to currency
.ActiveSheet.Range(ActiveSheet.Cells(7, 3), ActiveSheet.Cells(R, C)).NumberFormat = "$#,##0.00"
.ActiveSheet.Columns(3).AutoFit
.ActiveSheet.Range(ActiveSheet.Cells(R, 2), ActiveSheet.Cells(R, C)).Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
.ActiveSheet.Cells(R, 2).Font.Bold = True
R = R + 2
For i = 1 To rsMatrix.Fields.Count
.ActiveSheet.Cells(R, i + 2).Value = rsMatrix.Fields(i).Name
.ActiveSheet.Cells(R, i + 2).Font.Bold = True
.ActiveSheet.Columns(i + 2).ColumnWidth = 11.14
Next
rsMatrix.Close
cnCurrent.Close
MsgBox "Remember to save this Excel file", , "Jarvis FFE Database"
Set rsMatrix = Nothing
Set cnCurrent = Nothing
Set xlProjectCosts = Nothing
End With
End With
Exit Sub
ErrHandler:
If Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Number & Err.Description, , "Jarvis FFE Database"
Exit Sub
End If
End Sub
I posted this in the VBA forum yesterday and got some good help there, but the problem is not fixed. I have an Access query which, on a button click, is being exported to Excel. However I wanted the Excel sheet to be formatted in a certain way. I've got the code so that it works OK once, but if you try to run it again, I get one of a number of errors, but usually 1004: Method 'cells' of object '_Global' failed.
I've looked really hard at my code and I think it's fine - well it must be, to work once, but there is something wierd going on where it will only work again if I shut the db down and start it up again, or compact it.
Has anyone got any general ideas on what might be happening? I'm posting the code below. I don't think it's a specific Excel VBA coding issue, which is why I'm trying in this forum too....
Option Compare Database
Option Explicit
Dim xlProjectCosts As Excel.Application
Public Sub CreateLifeCycleReport(ProjectName As String, QueryName As String)
Dim cnCurrent As ADODB.Connection
Dim rsMatrix As ADODB.Recordset
Dim R As Long
Dim C As Integer
Dim i As Integer
Dim xlProjectCosts As Excel.Application
'Dim mWB As Excel.Workbook
On Error GoTo ErrHandler
Set cnCurrent = CurrentProject.Connection
Set rsMatrix = New ADODB.Recordset
rsMatrix.Open "SELECT * FROM " & QueryName, cnCurrent, adOpenDynamic, adLockPessimistic
Set xlProjectCosts = New Excel.Application
xlProjectCosts.Visible = True
xlProjectCosts.Workbooks.Add
'xlProjectCosts.ActiveWindow.DisplayGridlines = False
'Set mWB = ActiveWorkbook
'Enter details of project, autofit cells and put into bold type
With xlProjectCosts
With .ActiveWorkbook
.ActiveSheet.Cells(1, 1).Value = "Prepared:"
.ActiveSheet.Cells(1, 2).Value = Now()
.ActiveSheet.Columns(2).AutoFit
.ActiveSheet.Cells(3, 1).Value = "Project:"
.ActiveSheet.Cells(3, 2).Value = ProjectName
.ActiveSheet.Columns(2).AutoFit
.ActiveSheet.Range("A1:B3"
R = 5
'Enter Field names into row 5 (this is a crosstab query)
For i = 1 To rsMatrix.Fields.Count
.ActiveSheet.Cells(R, i + 2).Value = rsMatrix.Fields(i).Name
.ActiveSheet.Cells(R, i + 2).Font.Bold = True
.ActiveSheet.Columns(i + 2).ColumnWidth = 11.14
Next
'I want a bit of space between headings and the rest of the data so set current row to 7
R = 7
C = rsMatrix.Fields.Count + 2
'copy data from recordset - the queries I'm putting in all work fine
.ActiveSheet.Range("B7"
'the first column has text in so I'd like to leave it out of the formatting
R = .ActiveSheet.Range("C65536"
'select the range with numbers in and format to currency
.ActiveSheet.Range(ActiveSheet.Cells(7, 3), ActiveSheet.Cells(R, C)).NumberFormat = "$#,##0.00"
.ActiveSheet.Columns(3).AutoFit
.ActiveSheet.Range(ActiveSheet.Cells(R, 2), ActiveSheet.Cells(R, C)).Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
.ActiveSheet.Cells(R, 2).Font.Bold = True
R = R + 2
For i = 1 To rsMatrix.Fields.Count
.ActiveSheet.Cells(R, i + 2).Value = rsMatrix.Fields(i).Name
.ActiveSheet.Cells(R, i + 2).Font.Bold = True
.ActiveSheet.Columns(i + 2).ColumnWidth = 11.14
Next
rsMatrix.Close
cnCurrent.Close
MsgBox "Remember to save this Excel file", , "Jarvis FFE Database"
Set rsMatrix = Nothing
Set cnCurrent = Nothing
Set xlProjectCosts = Nothing
End With
End With
Exit Sub
ErrHandler:
If Err.Number = 3265 Then
Resume Next
Else
MsgBox Err.Number & Err.Description, , "Jarvis FFE Database"
Exit Sub
End If
End Sub