R
ryguy7272
I posted here about a week ago and haven't had time to come back to this
issue since then. I'm posting again so my question goes to the top of the
'list' and I'll link the old post to the new if I can find a solution via
this new post. Basically I have a few small subs that I use in Excel. I am
trying to figure out a way to just stay in Access, to save time, but do my
operations in Excel. Here is the code (with references to Excel):
Option Compare Database
Sub Rep()
'Open file
Dim objXL As Object
Dim xlFile As Object
Dim strWhat As String, boolXL As Boolean
Dim objActiveWkb As Object
Set objXL = CreateObject("Excel.Application")
boolXL = True
objXL.Application.Workbooks.Add
Set objActiveWkb = objXL.Application.ActiveWorkbook
xlFile = "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
objActiveWkb.Close savechanges:=True
If boolXL Then objXL.Application.Quit
Set objActiveWkb = Nothing: Set objXL = Nothing
MsgBox strWhat
End Sub
I do have a reference set to Excel!! Nevertheless, the code fails on this
line:
xlFile = "C:\Documents and Settings\ryan\Desktop\Mark\Mark - Union Crosstab
Rep.xls"
The message that I get is:
Run-time error ‘91’
Object variable or With block not set
What do I need to do to get this working?
Thanks,
Ryan---
issue since then. I'm posting again so my question goes to the top of the
'list' and I'll link the old post to the new if I can find a solution via
this new post. Basically I have a few small subs that I use in Excel. I am
trying to figure out a way to just stay in Access, to save time, but do my
operations in Excel. Here is the code (with references to Excel):
Option Compare Database
Sub Rep()
'Open file
Dim objXL As Object
Dim xlFile As Object
Dim strWhat As String, boolXL As Boolean
Dim objActiveWkb As Object
Set objXL = CreateObject("Excel.Application")
boolXL = True
objXL.Application.Workbooks.Add
Set objActiveWkb = objXL.Application.ActiveWorkbook
xlFile = "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
objActiveWkb.Close savechanges:=True
If boolXL Then objXL.Application.Quit
Set objActiveWkb = Nothing: Set objXL = Nothing
MsgBox strWhat
End Sub
I do have a reference set to Excel!! Nevertheless, the code fails on this
line:
xlFile = "C:\Documents and Settings\ryan\Desktop\Mark\Mark - Union Crosstab
Rep.xls"
The message that I get is:
Run-time error ‘91’
Object variable or With block not set
What do I need to do to get this working?
Thanks,
Ryan---