excel program when run second time from MS Access - fails giving object failed or method global row failed etc different error each time while stepping through code...
I think the cause is because of instance of excel is still seen in task manager process section.
Not able to figure out - what is wrong letting the excel still hang and not completely close out.
here is the code.
********
Public Sub Format_Report(whichquery As String)
Dim oapp As Object
Dim xlWkb As Object
Dim xlWks As Object
Dim iLastRow As Integer
Dim NumRowSearched As Integer
Set oapp = CreateObject("Excel.Application")
Set xlWkb = oapp.Workbooks.Open("C:\temp\TReport.xls")
Set xlWks = xlWkb.Worksheets("Sheet1")
Dim rval As String, I As Long
'insert rows in between and color fill
xlWkb.Worksheets("Sheet1").Select
rval = xlWks.Cells(1, 1): I = 2
Do Until Trim(xlWks.Cells(I, 1) & "") = ""
If xlWks.Cells(I, 1) <> rval Then
rval = xlWks.Cells(I, 1)
xlWks.Rows(I).Insert Shift:=xlDown
'color the row before
I = I + 1
'MsgBox i
xlWks.Rows(I - 1).EntireRow.Interior.ColorIndex = 17
End If
I = I + 1
Loop
oapp.Visible = True
iLastRow = xlWks.Cells(65536, 1).End(-4162).Row
'find the row that reads G Total
Dim sRow As Variant
sRow = oapp.WorksheetFunction.Match("G Total", xlWkb.Worksheets("sheet1").range("A1:A60"), 0)
If IsError(sRow) Then
'not found
err.Clear
End If
On Error GoTo 0
'Moves a row up or down in the worksheet
Dim rg As range
Dim myData As Variant, temp As Variant
Dim nRowSource As Long, nRowDestination As Long
nRowSource = sRow
nRowDestination = iLastRow
Set rg = xlWks.Rows(sRow)
xlWks.Rows(nRowDestination + 2).Insert
rg.Copy
xlWks.Rows(nRowDestination).PasteSpecial
rg.Delete
Dim Last As Long
Last = xlWkb.Worksheets("Sheet1").Cells.Find("*", SearchOrder:=xlByColumns, _
LookIn:=xlValues, SearchDirection:=xlPrevious).Column
'MsgBox Last
Dim X As Integer
For X = 1 To xlWkb.Worksheets("Sheet1").UsedRange.Columns.count
xlWkb.Worksheets("Sheet1").Columns(X).EntireColumn.AutoFit
Next X
oapp.ActiveWorkbook.RefreshAll
'xlWkb.Worksheets("Sheet1").AutoFit
With xlWks.Rows(1)
' .Font.Bold = True
.Interior.ColorIndex = 6
End With
'to avoid confusion variables are defined here place where they are being used.
Dim row_min As Integer
Dim row_max As Integer
Dim col_min As Integer
Dim col_max As Integer
' Select the used range.
xlWkb.Worksheets("Sheet1").UsedRange.Select
' Display the range's rows and columns.
row_min = xlWkb.Worksheets("Sheet1").UsedRange.Row
row_max = row_min + xlWkb.Worksheets("Sheet1").UsedRange.Rows.count - 1
col_min = xlWkb.Worksheets("Sheet1").UsedRange.Column
col_max = col_min + xlWkb.Worksheets("Sheet1").UsedRange.Columns.count - 1
' MsgBox row_min
'MsgBox row_max
'MsgBox col_min
'MsgBox col_max
Dim maxCol_name As String
Dim minCol_name As String
maxCol_name = get_columnName(col_max)
minCol_name = get_columnName(col_min)
'xlWkb.Worksheets("Sheet1").range("A1:1").EntireRow.Insert
Dim lastcell As range
'format as needed the margins, orientation, header, fit to page etc.
xlWkb.Worksheets("Sheet1").PageSetup.LeftMargin = oapp.InchesToPoints(0.1)
xlWkb.Worksheets("Sheet1").PageSetup.RightMargin = oapp.InchesToPoints(0.1)
xlWkb.Worksheets("Sheet1").PageSetup.Orientation = xlLandscape
xlWkb.Worksheets("sheet1").Cells.Font.Name = "Times New Roman"
If whichquery = "Combined" Then
'case disposition reports few columns, no review categories...hence column width may be increased
xlWkb.Worksheets("sheet1").range("D1" & ":" & maxCol_name & 1).ColumnWidth = 9.3
Else
xlWkb.Worksheets("sheet1").range("D1" & ":" & maxCol_name & 1).ColumnWidth = 5.29
End If
xlWkb.Worksheets("sheet1").Cells.Font.Size = 12
xlWkb.Worksheets("Sheet1").range(minCol_name & 1 & ":" & maxCol_name & 1).WrapText = True
xlWkb.Worksheets("Sheet1").range(minCol_name & ":" & maxCol_name).HorizontalAlignment = xlHAlignLeft
xlWkb.Worksheets("sheet1").PageSetup.FitToPagesWide = 1
xlWkb.Worksheets("sheet1").PageSetup.FitToPagesTall = 1
xlWkb.Worksheets("sheet1").PageSetup.Zoom = False
xlWkb.Worksheets("Sheet1").PageSetup.PrintArea = xlWkb.Worksheets("sheet1").UsedRange.address
xlWkb.Worksheets("Sheet1").PageSetup.PrintGridlines = True
If whichquery = "Combined" Then
Call DeleteColumns
Call Worksheet_Change
End If
'name the application according to the report name etc
Dim strtype As String
Dim strfile As String
strtype = Me!ReqType
If whichquery = "QY_ActiveToday_Final" Then
strfile = "c:\temp" & "\" & strtype & "_ActiveCases" & ".xls"
xlWkb.Worksheets("Sheet1").PageSetup.LeftHeader = strtype & " Case Load as of Today"
ElseIf whichquery = "Del_final" Then
strfile = "c:\temp" & "\" & strtype & "_DelinquentCases" & ".xls"
xlWkb.Worksheets("Sheet1").PageSetup.LeftHeader = strtype & " Delinquent Case Load as of Today"
ElseIf whichquery = "Combined" Then
strfile = "c:\temp" & "\" & strtype & "_Case Disposition" & ".xls"
xlWkb.Worksheets("Sheet1").PageSetup.LeftHeader = strtype & " Case Disposition Report"
End If
'chk if file exists
'MsgBox strfile
'save a copy of report in c:\temp
If FileExist(strfile) = True Then
Kill (strfile)
End If
ActiveWorkbook.SaveAs filename:=strfile
ActiveWorkbook.Close False
Set xlWkb = Nothing
Set xlWks = Nothing
Set oapp = Nothing
MsgBox "Done - Reports are saved in C:\Temp", vbOKOnly
End Sub
**************
Functions called from the sub above
Public Function get_columnName(colNo As Integer)
If colNo > 26 Then
get_columnName = Chr(Int((colNo - 1) / 26) + 64) & Chr(((colNo - 1) Mod 26) + 65)
Else
get_columnName = Chr(colNo + 64)
End If
End Function
'delete some unwanted columns from the sheet
Sub DeleteColumns()
Dim cell As range, DeleteRange As range
For Each cell In Intersect(ActiveSheet.Rows(1), ActiveSheet.UsedRange)
If cell Like "tblOngoing*" Or cell Like "tblActive*" Or cell = "New" Or cell = "Active" Or cell = "Ongoing" Then
If DeleteRange Is Nothing Then
Set DeleteRange = cell.EntireColumn
Else
Set DeleteRange = Union(DeleteRange, cell.EntireColumn)
End If
End If
Next
If Not DeleteRange Is Nothing Then DeleteRange.Delete
End Sub
Private Sub Worksheet_Change()
Worksheets("Sheet1").Cells(1, "A").Value = "Supervisor"
Worksheets("Sheet1").Cells(1, "B").Value = "Case Mgr"
End Sub
I think the cause is because of instance of excel is still seen in task manager process section.
Not able to figure out - what is wrong letting the excel still hang and not completely close out.
here is the code.
********
Public Sub Format_Report(whichquery As String)
Dim oapp As Object
Dim xlWkb As Object
Dim xlWks As Object
Dim iLastRow As Integer
Dim NumRowSearched As Integer
Set oapp = CreateObject("Excel.Application")
Set xlWkb = oapp.Workbooks.Open("C:\temp\TReport.xls")
Set xlWks = xlWkb.Worksheets("Sheet1")
Dim rval As String, I As Long
'insert rows in between and color fill
xlWkb.Worksheets("Sheet1").Select
rval = xlWks.Cells(1, 1): I = 2
Do Until Trim(xlWks.Cells(I, 1) & "") = ""
If xlWks.Cells(I, 1) <> rval Then
rval = xlWks.Cells(I, 1)
xlWks.Rows(I).Insert Shift:=xlDown
'color the row before
I = I + 1
'MsgBox i
xlWks.Rows(I - 1).EntireRow.Interior.ColorIndex = 17
End If
I = I + 1
Loop
oapp.Visible = True
iLastRow = xlWks.Cells(65536, 1).End(-4162).Row
'find the row that reads G Total
Dim sRow As Variant
sRow = oapp.WorksheetFunction.Match("G Total", xlWkb.Worksheets("sheet1").range("A1:A60"), 0)
If IsError(sRow) Then
'not found
err.Clear
End If
On Error GoTo 0
'Moves a row up or down in the worksheet
Dim rg As range
Dim myData As Variant, temp As Variant
Dim nRowSource As Long, nRowDestination As Long
nRowSource = sRow
nRowDestination = iLastRow
Set rg = xlWks.Rows(sRow)
xlWks.Rows(nRowDestination + 2).Insert
rg.Copy
xlWks.Rows(nRowDestination).PasteSpecial
rg.Delete
Dim Last As Long
Last = xlWkb.Worksheets("Sheet1").Cells.Find("*", SearchOrder:=xlByColumns, _
LookIn:=xlValues, SearchDirection:=xlPrevious).Column
'MsgBox Last
Dim X As Integer
For X = 1 To xlWkb.Worksheets("Sheet1").UsedRange.Columns.count
xlWkb.Worksheets("Sheet1").Columns(X).EntireColumn.AutoFit
Next X
oapp.ActiveWorkbook.RefreshAll
'xlWkb.Worksheets("Sheet1").AutoFit
With xlWks.Rows(1)
' .Font.Bold = True
.Interior.ColorIndex = 6
End With
'to avoid confusion variables are defined here place where they are being used.
Dim row_min As Integer
Dim row_max As Integer
Dim col_min As Integer
Dim col_max As Integer
' Select the used range.
xlWkb.Worksheets("Sheet1").UsedRange.Select
' Display the range's rows and columns.
row_min = xlWkb.Worksheets("Sheet1").UsedRange.Row
row_max = row_min + xlWkb.Worksheets("Sheet1").UsedRange.Rows.count - 1
col_min = xlWkb.Worksheets("Sheet1").UsedRange.Column
col_max = col_min + xlWkb.Worksheets("Sheet1").UsedRange.Columns.count - 1
' MsgBox row_min
'MsgBox row_max
'MsgBox col_min
'MsgBox col_max
Dim maxCol_name As String
Dim minCol_name As String
maxCol_name = get_columnName(col_max)
minCol_name = get_columnName(col_min)
'xlWkb.Worksheets("Sheet1").range("A1:1").EntireRow.Insert
Dim lastcell As range
'format as needed the margins, orientation, header, fit to page etc.
xlWkb.Worksheets("Sheet1").PageSetup.LeftMargin = oapp.InchesToPoints(0.1)
xlWkb.Worksheets("Sheet1").PageSetup.RightMargin = oapp.InchesToPoints(0.1)
xlWkb.Worksheets("Sheet1").PageSetup.Orientation = xlLandscape
xlWkb.Worksheets("sheet1").Cells.Font.Name = "Times New Roman"
If whichquery = "Combined" Then
'case disposition reports few columns, no review categories...hence column width may be increased
xlWkb.Worksheets("sheet1").range("D1" & ":" & maxCol_name & 1).ColumnWidth = 9.3
Else
xlWkb.Worksheets("sheet1").range("D1" & ":" & maxCol_name & 1).ColumnWidth = 5.29
End If
xlWkb.Worksheets("sheet1").Cells.Font.Size = 12
xlWkb.Worksheets("Sheet1").range(minCol_name & 1 & ":" & maxCol_name & 1).WrapText = True
xlWkb.Worksheets("Sheet1").range(minCol_name & ":" & maxCol_name).HorizontalAlignment = xlHAlignLeft
xlWkb.Worksheets("sheet1").PageSetup.FitToPagesWide = 1
xlWkb.Worksheets("sheet1").PageSetup.FitToPagesTall = 1
xlWkb.Worksheets("sheet1").PageSetup.Zoom = False
xlWkb.Worksheets("Sheet1").PageSetup.PrintArea = xlWkb.Worksheets("sheet1").UsedRange.address
xlWkb.Worksheets("Sheet1").PageSetup.PrintGridlines = True
If whichquery = "Combined" Then
Call DeleteColumns
Call Worksheet_Change
End If
'name the application according to the report name etc
Dim strtype As String
Dim strfile As String
strtype = Me!ReqType
If whichquery = "QY_ActiveToday_Final" Then
strfile = "c:\temp" & "\" & strtype & "_ActiveCases" & ".xls"
xlWkb.Worksheets("Sheet1").PageSetup.LeftHeader = strtype & " Case Load as of Today"
ElseIf whichquery = "Del_final" Then
strfile = "c:\temp" & "\" & strtype & "_DelinquentCases" & ".xls"
xlWkb.Worksheets("Sheet1").PageSetup.LeftHeader = strtype & " Delinquent Case Load as of Today"
ElseIf whichquery = "Combined" Then
strfile = "c:\temp" & "\" & strtype & "_Case Disposition" & ".xls"
xlWkb.Worksheets("Sheet1").PageSetup.LeftHeader = strtype & " Case Disposition Report"
End If
'chk if file exists
'MsgBox strfile
'save a copy of report in c:\temp
If FileExist(strfile) = True Then
Kill (strfile)
End If
ActiveWorkbook.SaveAs filename:=strfile
ActiveWorkbook.Close False
Set xlWkb = Nothing
Set xlWks = Nothing
Set oapp = Nothing
MsgBox "Done - Reports are saved in C:\Temp", vbOKOnly
End Sub
**************
Functions called from the sub above
Public Function get_columnName(colNo As Integer)
If colNo > 26 Then
get_columnName = Chr(Int((colNo - 1) / 26) + 64) & Chr(((colNo - 1) Mod 26) + 65)
Else
get_columnName = Chr(colNo + 64)
End If
End Function
'delete some unwanted columns from the sheet
Sub DeleteColumns()
Dim cell As range, DeleteRange As range
For Each cell In Intersect(ActiveSheet.Rows(1), ActiveSheet.UsedRange)
If cell Like "tblOngoing*" Or cell Like "tblActive*" Or cell = "New" Or cell = "Active" Or cell = "Ongoing" Then
If DeleteRange Is Nothing Then
Set DeleteRange = cell.EntireColumn
Else
Set DeleteRange = Union(DeleteRange, cell.EntireColumn)
End If
End If
Next
If Not DeleteRange Is Nothing Then DeleteRange.Delete
End Sub
Private Sub Worksheet_Change()
Worksheets("Sheet1").Cells(1, "A").Value = "Supervisor"
Worksheets("Sheet1").Cells(1, "B").Value = "Case Mgr"
End Sub