Runtime Error 1004: Method ‘Cells’ of Object ‘_Global’ failed

  • Thread starter ielmrani via AccessMonster.com
  • Start date
I

ielmrani via AccessMonster.com

Hi,
I am getting this error when I try to export to an excel sheet. When I click
on a button to export the first time it's fine, I rename the exported excel
sheet and I try to export it again and I get the error: Runtime Error 1004:
Method ‘Cells’ of Object ‘_Global’ failed

It highlight this line:

Range("A1:L1").Select

sorry the code behind the button is long:

im stDocName As String

stDocName = "QFinal4"
DoCmd.OutputTo acReport, stDocName

Dim xcelwb As Excel.Workbook
Dim xcelapp As Excel.Application
Dim Sheet As Excel.Worksheet

Set xcelapp = New Excel.Application
Set xcelwb = xcelapp.Workbooks.Open("QFinal4.xls")
Set Sheet = xcelapp.ActiveWorkbook.Sheets(1)

'format the excel data
With Sheet

Range("A1:L1").Select

Selection.Font.Bold = True
Selection.Font.ColorIndex = 54
Selection.Interior.ColorIndex = 36
Cells.Select
End With
With Selection.Font
.Name = "Tahoma"
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
End With
Cells.EntireColumn.AutoFit
Range("B2").Select
Selection.NumberFormat = "m/d/yyyy"
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Rows("1:2").Select
Selection.Insert Shift:=xlDown
Range("H1").Select
ActiveCell.FormulaR1C1 = "CD"
Range("H2").Select
ActiveCell.FormulaR1C1 = "Policy Summary By Owner"
Range("H1:H2").Select
Selection.Font.ColorIndex = 0
With Selection.Interior
.ColorIndex = 35
.Pattern = xlSolid
End With
Selection.Font.Bold = True
Columns("H:H").EntireColumn.AutoFit
Columns("H:H").ColumnWidth = 15
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Range("I8").Select
Columns("H:H").ColumnWidth = 18.43
Range("H1:I2").Select
Selection.Interior.ColorIndex = 35
Range("H19").Select
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("H1:I2").Select
Selection.Cut
Range("F1").Select
ActiveSheet.Paste
Columns("H:H").EntireColumn.AutoFit
Columns("G:G").ColumnWidth = 15.86
Columns("G:G").ColumnWidth = 17.57
Columns("F:F").ColumnWidth = 10.43
Columns("F:F").ColumnWidth = 11.86
Columns("G:G").ColumnWidth = 14.71
Range("J4").Select
ActiveCell.FormulaR1C1 = "Surrender Value"
Range("J4").Select
Selection.Font.Bold = True
Range("J4:J5").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With

With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("J4:J5").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("J5").Select
ActiveCell.FormulaR1C1 = " Date"
Range("J7").Select
Columns("J:J").EntireColumn.AutoFit
Range("A5:L5").Select
Range("L5").Activate
Selection.Font.Underline = xlUnderlineStyleSingle
Range("K23").Select
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
Range("J4").Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Selection.Interior.ColorIndex = 36
Range("J4").Select
Selection.Font.ColorIndex = 13
Range("J5").Select
Selection.Copy
Range("J4").Select

Range("C4:L13").Select
Selection.Cut
Range("A9").Select
ActiveSheet.Paste
Cells.Select
Cells.EntireColumn.AutoFit
Range("F1:F2").Select
Columns("F:F").ColumnWidth = 38.43
Range("G3").Select
Selection.Copy
Range("G1").Select

Selection.Copy
Range("G2").Select

Columns("F:F").EntireColumn.AutoFit
Range("B5").Select
Selection.Cut

Selection.ClearContents
Range("B4").Select
Selection.Copy
Range("B5").Select

Range("B6").Select
Selection.Cut
Range("A7").Select
ActiveSheet.Paste
Range("A5").Select
ActiveCell.FormulaR1C1 = "Insured Name/Date Of Birth"
Columns("B:B").Select
Columns("A:A").EntireColumn.AutoFit
Range("A18").Select

Range("G1").Select
Selection.Interior.ColorIndex = 2
Range("G2").Select
Selection.Interior.ColorIndex = 2
Range("B5").Select
Selection.Interior.ColorIndex = 2
Range("H9").Select
Selection.Font.ColorIndex = 53
Selection.Font.Underline = xlUnderlineStyleSingle
End With

Cells.Select
Cells.EntireRow.AutoFit
ActiveWorkbook.Save

End Sub

Thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top