Chance1234
IS-IT--Management
Hi
Im havingt trouble with the followng code throwing up a few automation problems here and there, i know theres a way round this using late binding or something but not sure how to implement this
Public Sub PsubCreatEGraphs(chkd(), chkid)
Dim i As Integer
Dim wrkapp As Excel.Application
Dim Wrkbknew As Workbook
Dim WrkShtNew As Worksheet
Dim sTrsQl As String
Dim rst As Recordset
Dim IntCo As Integer
Dim LpC As Integer
Dim ChtNew As Chart
Dim strb, strc As String
On Error GoTo ErrTrap
'-------------------Excel Add----------------
Set wrkapp = CreateObject("Excel.application"
Set Wrkbknew = wrkapp.Workbooks.Add
'Wrkbknew.Application.Visible = True
'----------****************** LOOP ***************************
LpC = 1
i = 1
Do Until LpC = UBound(chkd) + 1
Set WrkShtNew = Wrkbknew.Worksheets.Add
WrkShtNew.Name = chkd(LpC)
'--****-----Call Functions for SQL
sTrsQl = pfunWhAtSqL(chkd(LpC), chkid)
IntCo = pfunWhAtcolumns(chkd(LpC))
'--****----------------------------
'---** Connect TO Data **------------------
psubUserConnect
Set rst = objUserConnection.Execute(sTrsQl)
'--**POPULATE NEW SHEET WITH DATA FROM RST **____________
Do Until rst.EOF = True
Wrkbknew.Sheets(chkd(LpC)).Cells(i, 1) = rst.Fields(2)
Wrkbknew.Sheets(chkd(LpC)).Cells(i, 2) = 0 + rst.Fields(IntCo)
i = i + 1
rst.MoveNext
Loop
'------------------
'**** CREATE CHART ****----------------
If i > 1 Then
Set ChtNew = Wrkbknew.Charts.Add
strb = "B1:B" & i
strc = "!R1C1:R " & i & "C1"
With ChtNew
.ChartType = xlColumnClustered
.SetSourceData Source:=Sheets(chkd(LpC)).Range(strb), PlotBy:=xlColumns
.Location WHERE:=xlLocationAsObject, Name:=chkd(LpC)
End With
End If
'----********** Clear Up
Set WrkShtNew = Nothing
Set ChtNew = Nothing
sTrsQl = ""
IntCo = 0
strb = ""
strc = ""
i = 1
psubUserDisconnect
'=--------Loop counter increase
LpC = LpC + 1
Loop
Wrkbknew.SaveAs FileName:="C:\temp\tst1.xls"
Wrkbknew.Close
Set WrkShtNew = Nothing
Set ChtNew = Nothing
sTrsQl = ""
IntCo = 0
strb = ""
strc = ""
psubUserDisconnect
Set wrkapp = Nothing
Set Wrkbknew = Nothing
Exit Sub
ErrTrap:
Select Case Err.Number
Case 1044
Resume Next
Case Else
MsgBox Err.Description
End Select
End Sub
Im havingt trouble with the followng code throwing up a few automation problems here and there, i know theres a way round this using late binding or something but not sure how to implement this
Public Sub PsubCreatEGraphs(chkd(), chkid)
Dim i As Integer
Dim wrkapp As Excel.Application
Dim Wrkbknew As Workbook
Dim WrkShtNew As Worksheet
Dim sTrsQl As String
Dim rst As Recordset
Dim IntCo As Integer
Dim LpC As Integer
Dim ChtNew As Chart
Dim strb, strc As String
On Error GoTo ErrTrap
'-------------------Excel Add----------------
Set wrkapp = CreateObject("Excel.application"
Set Wrkbknew = wrkapp.Workbooks.Add
'Wrkbknew.Application.Visible = True
'----------****************** LOOP ***************************
LpC = 1
i = 1
Do Until LpC = UBound(chkd) + 1
Set WrkShtNew = Wrkbknew.Worksheets.Add
WrkShtNew.Name = chkd(LpC)
'--****-----Call Functions for SQL
sTrsQl = pfunWhAtSqL(chkd(LpC), chkid)
IntCo = pfunWhAtcolumns(chkd(LpC))
'--****----------------------------
'---** Connect TO Data **------------------
psubUserConnect
Set rst = objUserConnection.Execute(sTrsQl)
'--**POPULATE NEW SHEET WITH DATA FROM RST **____________
Do Until rst.EOF = True
Wrkbknew.Sheets(chkd(LpC)).Cells(i, 1) = rst.Fields(2)
Wrkbknew.Sheets(chkd(LpC)).Cells(i, 2) = 0 + rst.Fields(IntCo)
i = i + 1
rst.MoveNext
Loop
'------------------
'**** CREATE CHART ****----------------
If i > 1 Then
Set ChtNew = Wrkbknew.Charts.Add
strb = "B1:B" & i
strc = "!R1C1:R " & i & "C1"
With ChtNew
.ChartType = xlColumnClustered
.SetSourceData Source:=Sheets(chkd(LpC)).Range(strb), PlotBy:=xlColumns
.Location WHERE:=xlLocationAsObject, Name:=chkd(LpC)
End With
End If
'----********** Clear Up
Set WrkShtNew = Nothing
Set ChtNew = Nothing
sTrsQl = ""
IntCo = 0
strb = ""
strc = ""
i = 1
psubUserDisconnect
'=--------Loop counter increase
LpC = LpC + 1
Loop
Wrkbknew.SaveAs FileName:="C:\temp\tst1.xls"
Wrkbknew.Close
Set WrkShtNew = Nothing
Set ChtNew = Nothing
sTrsQl = ""
IntCo = 0
strb = ""
strc = ""
psubUserDisconnect
Set wrkapp = Nothing
Set Wrkbknew = Nothing
Exit Sub
ErrTrap:
Select Case Err.Number
Case 1044
Resume Next
Case Else
MsgBox Err.Description
End Select
End Sub