bibig1978
Sep 24th, 2004, 01:01 PM
I am creating a macro on analyst activity in my group. My output has four columns on excel: Takenby, Completed Emails, Open Email Tickets and Total. Below is how the output looks:
Takenby
Kit
Patricia
Tabitha
John
Katie
Completed Emails
7
1
0
7
1
Open Email Tickets
2
4
3
4
1
I need to somehow add up the Completed Emails to Open Email Tickets to get the following ouput for the Totals column:
Total
9
5
3
11
2
Can somebody please help me? I'm fairly new to VB coding and welcome any help I can get. Thanks.
This is the code i have so far:
Sub NewReport()
'
' NewReport Macro
' Macro recorded 8/20/2004 by bevithag
'
Dim conn As ADODB.Connection
Dim ConnectionString As String
Set conn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim rs1 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
Dim SqlString As String
Dim xl As Excel.Application
Dim xlw As Excel.Workbook
Dim xls As Excel.Worksheets
Dim intRows As Integer
conn.CursorLocation = adUseClient
ConnectionString = "DRIVER=ODBC;Data Source=csathenacms;NA=172.27.101.179, 5000;Database=CMS;UID=Bevithag;PWD=bevithag"
conn.Open ConnectionString 'Open connection
'test to see if connection works
If conn.State = adStateOpen Then
MsgBox "You are now connected to the database"
ElseIf conn.State = adStateClose Then
MsgBox "You are not connected to the database"
End If
'Open recordset query
rs.Open "Select takenby AS Takenby," & Chr(13) & "" & Chr(10) & _
"(Select count(*)" & Chr(13) & "" & Chr(10) & _
"FROM CMS.dbo.disp" & Chr(13) & "" & Chr(10) & _
"WHERE takenby=a.takenby and status = 'Completed' and source='Email' and opendte=Current_Date())," & Chr(13) & "" & Chr(10) & _
"(Select count(*)" & Chr(13) & "" & Chr(10) & _
"from CMS.dbo.disp " & Chr(13) & "" & Chr(10) & _
"where takenby = a.takenby and status != 'Completed' and source = 'Email' and opendte = Current_Date()) " & Chr(13) & "" & Chr(10) & _
"from CMS.dbo.disp a " & Chr(13) & "" & Chr(10) & _
"where opendte = Current_Date() " & Chr(13) & "" & Chr(10) & _
"Group By takenby", conn, adOpenStatic, adLockOptimistic
Set xl = Excel.Application
Range("F2").Select
ActiveCell.FormulaR1C1 = _
"CMS Support Report on Analyst Activity"
With ActiveCell.Characters(Start:=1, Length:=53).Font
.Name = "Arial"
.FontStyle = "Italic"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("F2").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
MsgBox "Open Workbook"
xl.Worksheets("Analyst-Activity").Select
Columns("B:B").Select
Selection.ColumnWidth = 15#
Selection.WrapText = True
Range("B4").Select
ActiveCell.FormulaR1C1 = "Completed Emails"
'Set the focus back at the first cell
Columns("C:C").Select
Selection.ColumnWidth = 15#
Selection.WrapText = True
Range("C4").Select
ActiveCell.FormulaR1C1 = "Open Emails"
'Set the focus back at the first cell
Columns("D:D").Select
Selection.ColumnWidth = 15#
Selection.WrapText = True
Range("D4").Select
ActiveCell.FormulaR1C1 = "Total"
'Open existing worksheet
MsgBox "Open Worksheet"
With xls
'copy recordset onto excel
xl.Range("A5").CopyFromRecordset rs
Set xlw = Nothing
Set xl = Nothing
End With
conn.Close
Set conn = Nothing
End Sub
Takenby
Kit
Patricia
Tabitha
John
Katie
Completed Emails
7
1
0
7
1
Open Email Tickets
2
4
3
4
1
I need to somehow add up the Completed Emails to Open Email Tickets to get the following ouput for the Totals column:
Total
9
5
3
11
2
Can somebody please help me? I'm fairly new to VB coding and welcome any help I can get. Thanks.
This is the code i have so far:
Sub NewReport()
'
' NewReport Macro
' Macro recorded 8/20/2004 by bevithag
'
Dim conn As ADODB.Connection
Dim ConnectionString As String
Set conn = New ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim rs1 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
Dim SqlString As String
Dim xl As Excel.Application
Dim xlw As Excel.Workbook
Dim xls As Excel.Worksheets
Dim intRows As Integer
conn.CursorLocation = adUseClient
ConnectionString = "DRIVER=ODBC;Data Source=csathenacms;NA=172.27.101.179, 5000;Database=CMS;UID=Bevithag;PWD=bevithag"
conn.Open ConnectionString 'Open connection
'test to see if connection works
If conn.State = adStateOpen Then
MsgBox "You are now connected to the database"
ElseIf conn.State = adStateClose Then
MsgBox "You are not connected to the database"
End If
'Open recordset query
rs.Open "Select takenby AS Takenby," & Chr(13) & "" & Chr(10) & _
"(Select count(*)" & Chr(13) & "" & Chr(10) & _
"FROM CMS.dbo.disp" & Chr(13) & "" & Chr(10) & _
"WHERE takenby=a.takenby and status = 'Completed' and source='Email' and opendte=Current_Date())," & Chr(13) & "" & Chr(10) & _
"(Select count(*)" & Chr(13) & "" & Chr(10) & _
"from CMS.dbo.disp " & Chr(13) & "" & Chr(10) & _
"where takenby = a.takenby and status != 'Completed' and source = 'Email' and opendte = Current_Date()) " & Chr(13) & "" & Chr(10) & _
"from CMS.dbo.disp a " & Chr(13) & "" & Chr(10) & _
"where opendte = Current_Date() " & Chr(13) & "" & Chr(10) & _
"Group By takenby", conn, adOpenStatic, adLockOptimistic
Set xl = Excel.Application
Range("F2").Select
ActiveCell.FormulaR1C1 = _
"CMS Support Report on Analyst Activity"
With ActiveCell.Characters(Start:=1, Length:=53).Font
.Name = "Arial"
.FontStyle = "Italic"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("F2").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
MsgBox "Open Workbook"
xl.Worksheets("Analyst-Activity").Select
Columns("B:B").Select
Selection.ColumnWidth = 15#
Selection.WrapText = True
Range("B4").Select
ActiveCell.FormulaR1C1 = "Completed Emails"
'Set the focus back at the first cell
Columns("C:C").Select
Selection.ColumnWidth = 15#
Selection.WrapText = True
Range("C4").Select
ActiveCell.FormulaR1C1 = "Open Emails"
'Set the focus back at the first cell
Columns("D:D").Select
Selection.ColumnWidth = 15#
Selection.WrapText = True
Range("D4").Select
ActiveCell.FormulaR1C1 = "Total"
'Open existing worksheet
MsgBox "Open Worksheet"
With xls
'copy recordset onto excel
xl.Range("A5").CopyFromRecordset rs
Set xlw = Nothing
Set xl = Nothing
End With
conn.Close
Set conn = Nothing
End Sub