Hello all.

I'm brand new to this forum, so I hope I'm posting this in the right section.

I've established code that will export data to excel into a workbook with
tabs(worksheets) for each month(based on a field expdate). What I'd like to
do is make tabs not only based on month, but on year as well, for the
following reason:

Basically, each group of customers is generally in a 12 month time-frame;
however, there are some exceptions to the rule that extend into the previous
year and the upcoming year. So, my ideal set-up would be to probably create
worksheets with outer limits that only extend to months that contain records.
So, if there was a policy that had an exp date of 12/24/05 and the rest were
in '06, the output would only create a dec 2005 and not a nov2005 or any
other 2005 because they didn't contain records. Hope this kind of helps w the
explanation.

Here is the code I've been working with (I didn't set it all up, much is
taken from online examples, but the field names are mine and part of my test
database)

VB Code:
  1. Sub CreateXL()
  2. Dim strSQL As String
  3. Dim qdf As Object
  4. Dim strFilename As String
  5. Dim I As Long
  6. Dim Yr As Long
  7. Dim YearFirst As Long
  8. Dim YearLast As Long
  9. Dim resp
  10.  
  11.    strFilename = "C:\" & [Forms]![Form]![TC] & ".xls"
  12.    
  13.    If Dir(strFilename) <> "" Then
  14.        resp = MsgBox("This group's import already exists." & vbCrLf & "Do
  15. you wish to replace it?", vbYesNo)
  16.        If resp = vbYes Then
  17.            Kill strFilename
  18.        Else
  19.            Exit Sub
  20.        End If
  21.    End If
  22.    
  23.    YearFirst = DMin("Year(ExpDate)", "Table1")
  24.    YearLast = DMax("Year(ExpDate)", "Table1")
  25.    For Yr = YearFirst To YearLast
  26.        If DCount("Year(Expdate)", "Table1", "Year(Expdate)=" & Yr) > 0 Then
  27.        
  28.            For I = 1 To 12
  29.                If DCount("Month(Expdate)", "Table1", "Month(Expdate)=" & I)
  30.  
  31. > 0 Then
  32.                    strSQL = "SELECT  Table1.Customer, Table1.ZipCode, Table1.
  33. ItemType, Table1.ExpDate "
  34.                    strSQL = strSQL & "FROM Table2 INNER JOIN Table1 ON
  35. Table2.GroupCode = Table1.GroupCode "
  36.                    strSQL = strSQL & "WHERE Table1.GroupCode='" & [Forms]!
  37. [Form]![TC] & "' AND Month(Table1.Expdate)= " & I & " AND Year(Table1.Expdate)
  38. = " & Yr
  39.                    strSQL = strSQL & " ORDER BY Table1.Customer"
  40.                    Set qdf = CurrentDb.CreateQueryDef(Format(DateSerial(2006,
  41. I, 1), "mmm") & Yr, strSQL)
  42.                    DoCmd.TransferSpreadsheet acExport,
  43. acSpreadsheetTypeExcel9, qdf.Name, strFilename
  44.                    CurrentDb.QueryDefs.Delete qdf.Name
  45.                End If
  46.            Next I
  47.            
  48.        End If
  49.    Next Yr
  50.    FormatWB strFilename
  51. End Sub
  52. Sub FormatWB(strFilename As String)
  53. Dim xlApp As Object
  54. Dim xlWB As Object
  55. Dim xlWS As Object
  56.    Set xlApp = CreateObject("Excel.Application")
  57.    Set xlWB = xlApp.Workbooks.Open(strFilename)
  58.    For Each xlWS In xlWB.Worksheets
  59.        xlWS.Range("A1:L1").Font.Bold = True
  60.        xlWS.Range("A:L").Columns.AutoFit
  61.        xlWS.Range("1:1").Insert
  62.        With xlWS.Range("A1")
  63.            .Value = [Forms]![Form]![TC]
  64.            .Font.Size = 24
  65.            .Font.Bold = True
  66.        End With
  67.    Next xlWS
  68.    xlWB.Close True
  69.    DoCmd.Close ' Close Form
  70. End Sub

Thanks for any and all help. Right now I can't really tell what range of months my
code is taking, but my goal was to only have wksht tabs that extend to the
outter limits of the exp date field.

If anyone would like me to attach my sample db let me know. It would be a lifesaver if someone could sort through all this.