bibig1978
Dec 13th, 2004, 01:11 PM
I am creating an excel macro on the number of analysts (by group) that has logged into our database for the current day. However when I open up my excel sheet, I would like to see the macro keep the existing data (previous day) and display the data of the current date in the next column. My output consists of group and date (number of analysts from each group logging into the database).
Below is how the output looks. Right now i can only get my macro to look at the current day.
Group November 30 (current date)
PC 8
MD 17
SYS 1
I have not been able to figure out how to keep the existing data and display the next day's data onto the next column such as the output below:
Group November 30 Dec 1st Dec 2nd Dec 3rd
PC 8 12 0 15
MD 17 5 12 2
SYS 1 9 14 3
Can somebody please help me? I believe that I may need to run a loop through my query set. I'm fairly new to VB coding and welcome any help I can get. Thanks.
This is the code i have so far:
Sub UsageReport()
'
' NewReport Macro
' Macro recorded 8/20/2004
'
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=cmsreports;PWD=reports"
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 A.GROUP2, Count(B.USERID) as Dte" & Chr(13) & "" & Chr(10) & _
"FROM cms.dbo.sys_user A, CMS.DBO.USAGELOG B" & Chr(13) & "" & Chr(10) & _
"WHERE B.USERID = A.USER_ID AND ((B.LOGIN_DT=Current_date()) AND (B.ISACTIVE=1))" & Chr(13) & "" & Chr(10) & _
"GROUP BY A.GROUP2" & Chr(13) & "" & Chr(10) & _
"ORDER BY A.GROUP2", conn, adOpenStatic, adLockOptimistic
Set xl = Excel.Application
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"USAGE LOG REPORT" 'Title of Report
With ActiveCell.Characters(Start:=1, Length:=53).Font
.Name = "Comic Sans MS"
.Size = 13
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
'Range("A2").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
' MsgBox "Open Workbook"
xl.Worksheets("Usage").Select 'set parameters for each column A-J
Columns("A:A").Select
Selection.ColumnWidth = 25#
Selection.WrapText = True
Range("A4").Select 'name of column
ActiveCell.FormulaR1C1 = "Group"
'Open existing worksheet
'MsgBox "Open Worksheet"
With xls
xl.Range("A5").CopyFromRecordset rs
Set xlw = Nothing
Set xl = Nothing
End With
conn.Close
Set conn = Nothing
End Sub
Below is how the output looks. Right now i can only get my macro to look at the current day.
Group November 30 (current date)
PC 8
MD 17
SYS 1
I have not been able to figure out how to keep the existing data and display the next day's data onto the next column such as the output below:
Group November 30 Dec 1st Dec 2nd Dec 3rd
PC 8 12 0 15
MD 17 5 12 2
SYS 1 9 14 3
Can somebody please help me? I believe that I may need to run a loop through my query set. I'm fairly new to VB coding and welcome any help I can get. Thanks.
This is the code i have so far:
Sub UsageReport()
'
' NewReport Macro
' Macro recorded 8/20/2004
'
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=cmsreports;PWD=reports"
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 A.GROUP2, Count(B.USERID) as Dte" & Chr(13) & "" & Chr(10) & _
"FROM cms.dbo.sys_user A, CMS.DBO.USAGELOG B" & Chr(13) & "" & Chr(10) & _
"WHERE B.USERID = A.USER_ID AND ((B.LOGIN_DT=Current_date()) AND (B.ISACTIVE=1))" & Chr(13) & "" & Chr(10) & _
"GROUP BY A.GROUP2" & Chr(13) & "" & Chr(10) & _
"ORDER BY A.GROUP2", conn, adOpenStatic, adLockOptimistic
Set xl = Excel.Application
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"USAGE LOG REPORT" 'Title of Report
With ActiveCell.Characters(Start:=1, Length:=53).Font
.Name = "Comic Sans MS"
.Size = 13
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
'Range("A2").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
' MsgBox "Open Workbook"
xl.Worksheets("Usage").Select 'set parameters for each column A-J
Columns("A:A").Select
Selection.ColumnWidth = 25#
Selection.WrapText = True
Range("A4").Select 'name of column
ActiveCell.FormulaR1C1 = "Group"
'Open existing worksheet
'MsgBox "Open Worksheet"
With xls
xl.Range("A5").CopyFromRecordset rs
Set xlw = Nothing
Set xl = Nothing
End With
conn.Close
Set conn = Nothing
End Sub