Hi all,
I have posted here before and most of the time, I have gotten the help that I needed. We have the following macro that creates a report. The macro:
1) Open a template (Excel file) and copy the same (4) columns from one sheet named “Sheet 1” to another one named “Table 1” multiple times.
2) For each set of columns (above), associate one record from a field (Desk) in a table ( Access database)
3) Then, populate the report.
4) Finally repeat steps (1-3) for 3 other tables in the same template. Thus, at the end, the report should have 5 sheets: “Sheet1”, “Table 1”, “Table 2”, “Table 3”, “Table 4”
However, my macro returns more than 256 columns per sheet because the total number of columns is equal to the number of records in the field in MS Access times the 4 columns in “Sheet 1”. It works fine in Excel 2007 but obviously doesn’t work in Excel 2003 because of the limitations. I would like the macro to place the first 254 fields/columns on one sheet in the workbook and the remaining fields (beginning with the 255th field) on a second sheet in the workbook…The report would look like this: “Sheet1”, “Table 1”, “Table 1b”, “Table 2”, “Table 2b”, etc.
However, I have tried to come out with the proper code but I’m very limited with excel. In addition, this macro was designed by somebody else who doesn’t work at my company anymore. Your help will be greatly appreciated.
Thanks very much,
B.
The code below is for only one of the tables:
Dim db As Database
Dim wrkJet As Workspace
Dim datCurWeek As Date
Dim lngRow As Long
Dim J As Integer
Sub PopulateReport()
Dim strOutputFile As String
Dim strOutputFileName As String
Dim I As Integer
Dim intYear As Integer
Dim intCurYear
Call DBOpen
strOutputFile = Range("rngOutputFile")
FileCopy "C:\Report Template.xlsx", strOutputFile
strOutputFileName = Right(strOutputFile, _
Len(strOutputFile) - InStrRev(strOutputFile, "\"))
Workbooks.Open strOutputFile
Workbooks(strOutputFileName).Activate
' lngRow = 3
' intYear = Year(Now)
' intCurYear = Year(Now)
' datCurWeek = Now - Weekday(Now) + 2
' While intYear = intCurYear
' Range("A" & lngRow) = datCurWeek
Call GetTable1Data
Call GetTable2Data
Call GetTable3Data
Call GetTable4Data
' datCurWeek = datCurWeek - 7
' intCurYear = Year(datCurWeek)
' lngRow = lngRow + 1
' Wend
' Set objExcel = Nothing
Workbooks(strOutputFileName).Sheets("Sheet1").Visible = False
End Sub
Sub DBOpen()
Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set db = wrkJet.OpenDatabase("C:\MyAccess.mdb", False)
End Sub
Sub GetTable1Data()
Dim strSQL As String
Dim rs As Recordset
Dim rsDesk As Recordset
Dim I As Integer
Dim intYear As Integer
Dim intCurYear
Sheets("Sheet1").Select
Range("AA:AD").Select
Selection.Copy
Sheets("Table1").Select
lngRow = 3
intYear = Year(Now)
' intYear = 2008
intCurYear = Year(Now)
datCurWeek = Now - Weekday(Now) + 2
strSQL = "SELECT DISTINCT Table1.Desk FROM Table1"
Set rsDesk = db.OpenRecordset(strSQL)
rsDesk.MoveFirst
I = 2
While Not rsDesk.EOF
Sheets("Table1").Cells(1, I).Select
Sheets("Table1").Paste
Sheets("Table1").Cells(1, I) = rsDesk(0)
I = I + 4
rsDesk.MoveNext
Wend
While intYear = intCurYear
Range("A" & lngRow) = Format(datCurWeek, "mm/dd/yyyy")
rsDesk.MoveFirst
I = 2
While Not rsDesk.EOF
' Sheets("Table1").Cells(1, I).Select
' Sheets("Table1").Paste
'
' Sheets("Table1").Cells(1, I) = rsDesk(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate >= #" & Format(datCurWeek, "mm/dd/yyyy") & "#) And (Table1.AmendDate < #" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) And (Table1.Status = 'Ver') AND " & _
"(Table1.ChangeType='Cancelled') And (Table1.Desk = '" & rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I) = rs(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate >= #" & Format(datCurWeek, "mm/dd/yyyy") & "#) And (Table1.AmendDate < #" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) And (Table1.Status = 'Ver') And (Table1.Desk = '" & rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I + 1) = rs(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate>=#" & Format(datCurWeek, "mm/dd/yyyy") & "#) AND (Table1.AmendDate<#" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) AND (Table1.Status='Ver') AND ((Table1.ChangeType)='Customer') And ((Table1.Desk) = '" & _
rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I + 2) = rs(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate>=#" & Format(datCurWeek, "mm/dd/yyyy") & "#) AND (Table1.AmendDate<#" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) AND (Table1.Status='Ver') AND ((Table1.ChangeType='EffDate') OR (Table1.ChangeType='StartDate')) And (Table1.Desk = '" & _
rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I + 3) = rs(0)
rsDesk.MoveNext
For J = 0 To 3
If Sheets("Table1").Cells(lngRow, I + J) > 0 Then
Sheets("Table1").Cells(lngRow, I + J).Interior.ColorIndex = 3
Sheets("Table1").Cells(lngRow, I + J).Interior.Pattern = xlSolid
End If
Next J
I = I + 4
Wend
datCurWeek = datCurWeek - 7
intCurYear = Year(datCurWeek)
lngRow = lngRow + 1
Wend
End Sub
I have posted here before and most of the time, I have gotten the help that I needed. We have the following macro that creates a report. The macro:
1) Open a template (Excel file) and copy the same (4) columns from one sheet named “Sheet 1” to another one named “Table 1” multiple times.
2) For each set of columns (above), associate one record from a field (Desk) in a table ( Access database)
3) Then, populate the report.
4) Finally repeat steps (1-3) for 3 other tables in the same template. Thus, at the end, the report should have 5 sheets: “Sheet1”, “Table 1”, “Table 2”, “Table 3”, “Table 4”
However, my macro returns more than 256 columns per sheet because the total number of columns is equal to the number of records in the field in MS Access times the 4 columns in “Sheet 1”. It works fine in Excel 2007 but obviously doesn’t work in Excel 2003 because of the limitations. I would like the macro to place the first 254 fields/columns on one sheet in the workbook and the remaining fields (beginning with the 255th field) on a second sheet in the workbook…The report would look like this: “Sheet1”, “Table 1”, “Table 1b”, “Table 2”, “Table 2b”, etc.
However, I have tried to come out with the proper code but I’m very limited with excel. In addition, this macro was designed by somebody else who doesn’t work at my company anymore. Your help will be greatly appreciated.
Thanks very much,
B.
The code below is for only one of the tables:
Dim db As Database
Dim wrkJet As Workspace
Dim datCurWeek As Date
Dim lngRow As Long
Dim J As Integer
Sub PopulateReport()
Dim strOutputFile As String
Dim strOutputFileName As String
Dim I As Integer
Dim intYear As Integer
Dim intCurYear
Call DBOpen
strOutputFile = Range("rngOutputFile")
FileCopy "C:\Report Template.xlsx", strOutputFile
strOutputFileName = Right(strOutputFile, _
Len(strOutputFile) - InStrRev(strOutputFile, "\"))
Workbooks.Open strOutputFile
Workbooks(strOutputFileName).Activate
' lngRow = 3
' intYear = Year(Now)
' intCurYear = Year(Now)
' datCurWeek = Now - Weekday(Now) + 2
' While intYear = intCurYear
' Range("A" & lngRow) = datCurWeek
Call GetTable1Data
Call GetTable2Data
Call GetTable3Data
Call GetTable4Data
' datCurWeek = datCurWeek - 7
' intCurYear = Year(datCurWeek)
' lngRow = lngRow + 1
' Wend
' Set objExcel = Nothing
Workbooks(strOutputFileName).Sheets("Sheet1").Visible = False
End Sub
Sub DBOpen()
Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set db = wrkJet.OpenDatabase("C:\MyAccess.mdb", False)
End Sub
Sub GetTable1Data()
Dim strSQL As String
Dim rs As Recordset
Dim rsDesk As Recordset
Dim I As Integer
Dim intYear As Integer
Dim intCurYear
Sheets("Sheet1").Select
Range("AA:AD").Select
Selection.Copy
Sheets("Table1").Select
lngRow = 3
intYear = Year(Now)
' intYear = 2008
intCurYear = Year(Now)
datCurWeek = Now - Weekday(Now) + 2
strSQL = "SELECT DISTINCT Table1.Desk FROM Table1"
Set rsDesk = db.OpenRecordset(strSQL)
rsDesk.MoveFirst
I = 2
While Not rsDesk.EOF
Sheets("Table1").Cells(1, I).Select
Sheets("Table1").Paste
Sheets("Table1").Cells(1, I) = rsDesk(0)
I = I + 4
rsDesk.MoveNext
Wend
While intYear = intCurYear
Range("A" & lngRow) = Format(datCurWeek, "mm/dd/yyyy")
rsDesk.MoveFirst
I = 2
While Not rsDesk.EOF
' Sheets("Table1").Cells(1, I).Select
' Sheets("Table1").Paste
'
' Sheets("Table1").Cells(1, I) = rsDesk(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate >= #" & Format(datCurWeek, "mm/dd/yyyy") & "#) And (Table1.AmendDate < #" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) And (Table1.Status = 'Ver') AND " & _
"(Table1.ChangeType='Cancelled') And (Table1.Desk = '" & rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I) = rs(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate >= #" & Format(datCurWeek, "mm/dd/yyyy") & "#) And (Table1.AmendDate < #" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) And (Table1.Status = 'Ver') And (Table1.Desk = '" & rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I + 1) = rs(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate>=#" & Format(datCurWeek, "mm/dd/yyyy") & "#) AND (Table1.AmendDate<#" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) AND (Table1.Status='Ver') AND ((Table1.ChangeType)='Customer') And ((Table1.Desk) = '" & _
rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I + 2) = rs(0)
strSQL = "SELECT Count(Table1.TradeID) AS CountOfTradeID " & _
"FROM Table1 " & _
"WHERE ((Table1.AmendDate>=#" & Format(datCurWeek, "mm/dd/yyyy") & "#) AND (Table1.AmendDate<#" & _
Format((datCurWeek + 7), "mm/dd/yyyy") & "#) AND (Table1.Status='Ver') AND ((Table1.ChangeType='EffDate') OR (Table1.ChangeType='StartDate')) And (Table1.Desk = '" & _
rsDesk(0) & "'))"
Set rs = db.OpenRecordset(strSQL)
If Not (rs.BOF And rs.EOF) Then Sheets("Table1").Cells(lngRow, I + 3) = rs(0)
rsDesk.MoveNext
For J = 0 To 3
If Sheets("Table1").Cells(lngRow, I + J) > 0 Then
Sheets("Table1").Cells(lngRow, I + J).Interior.ColorIndex = 3
Sheets("Table1").Cells(lngRow, I + J).Interior.Pattern = xlSolid
End If
Next J
I = I + 4
Wend
datCurWeek = datCurWeek - 7
intCurYear = Year(datCurWeek)
lngRow = lngRow + 1
Wend
End Sub