R
ryguy7272
I am trying to open an Excel file from Access, make changes, save the file,
and close the file. When I run the code in Excel, it works fine. I figured
if I can run this in Access, I can learn something new and save myself some
time during my increasingly busy days.
Option Compare Database
Sub Rep()
'Open file
Dim xlapp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim xlrng As Excel.Range
Dim adors As ADODB.Recordset
Dim xlFile As String
Dim xlSaveFile As String
xlFile = "C:\Documents and Settings\ryan\Desktop\Mark\Mark - Union Crosstab
Rep.xls"
xlSaveFile = "C:\Documents and Settings\ryan\Desktop\Mark\Mark - Union
Crosstab Rep.xls"
'Begin formatting
Columns("F:F").Select
Selection.Cut
Columns("H:H").Select
ActiveSheet.Paste
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Font.Bold = True
Cells.Select
Cells.EntireColumn.AutoFit
Range("A2").Select
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(5, 6,
7), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Call Calc1
End Sub
Sub Calc1()
Range("H2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-4]="""",RC[-1]=0),""Goal is
Zero"",IF(RC[-4]="""",((RC[-3]+RC[-2])/RC[-1]),""""))"
Range("H3").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 7).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Style = "Percent"
Columns("E:G").Select
Selection.Style = "Currency"
Call PlaceBottomDoubleBorderLines1
End Sub
Sub PlaceBottomDoubleBorderLines1()
Dim C As Range
For Each C In Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row)
If C.Font.Bold Then
With C.Resize(, 8).Borders(xlEdgeBottom)
..LineStyle = xlDouble
..Weight = xlThick
..ColorIndex = xlAutomatic
End With
End If
Next
'Save changes and close file
xlrng.CopyFromRecordset adors
adors.Close
Set adors = Nothing
xlwb.SaveAs xlSaveFile
xlwb.Close
Set xlrng = Nothing
Set xlws = Nothing
Set xlwb = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
The error message is: Run-time error 1004
Method 'Columns' of object '_Global failed.
Any ideas?
Thanks so much,
Ryan--
and close the file. When I run the code in Excel, it works fine. I figured
if I can run this in Access, I can learn something new and save myself some
time during my increasingly busy days.
Option Compare Database
Sub Rep()
'Open file
Dim xlapp As Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim xlrng As Excel.Range
Dim adors As ADODB.Recordset
Dim xlFile As String
Dim xlSaveFile As String
xlFile = "C:\Documents and Settings\ryan\Desktop\Mark\Mark - Union Crosstab
Rep.xls"
xlSaveFile = "C:\Documents and Settings\ryan\Desktop\Mark\Mark - Union
Crosstab Rep.xls"
'Begin formatting
Columns("F:F").Select
Selection.Cut
Columns("H:H").Select
ActiveSheet.Paste
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Font.Bold = True
Cells.Select
Cells.EntireColumn.AutoFit
Range("A2").Select
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(5, 6,
7), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Call Calc1
End Sub
Sub Calc1()
Range("H2").Select
ActiveCell.FormulaR1C1 = _
"=IF(AND(RC[-4]="""",RC[-1]=0),""Goal is
Zero"",IF(RC[-4]="""",((RC[-3]+RC[-2])/RC[-1]),""""))"
Range("H3").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 7).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Selection.Style = "Percent"
Columns("E:G").Select
Selection.Style = "Currency"
Call PlaceBottomDoubleBorderLines1
End Sub
Sub PlaceBottomDoubleBorderLines1()
Dim C As Range
For Each C In Range("A1").Resize(Cells(Rows.Count, "A").End(xlUp).Row)
If C.Font.Bold Then
With C.Resize(, 8).Borders(xlEdgeBottom)
..LineStyle = xlDouble
..Weight = xlThick
..ColorIndex = xlAutomatic
End With
End If
Next
'Save changes and close file
xlrng.CopyFromRecordset adors
adors.Close
Set adors = Nothing
xlwb.SaveAs xlSaveFile
xlwb.Close
Set xlrng = Nothing
Set xlws = Nothing
Set xlwb = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
The error message is: Run-time error 1004
Method 'Columns' of object '_Global failed.
Any ideas?
Thanks so much,
Ryan--