Results 1 to 19 of 19

Thread: [RESOLVED] Export To Excel into Wkshts by mnth and yr

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Resolved [RESOLVED] Export To Excel into Wkshts by mnth and yr

    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.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    ok..
    first:

    second: yes.. this is the correct forum

    third: your code...

    its hard to tell.. but it seems like you are exporting spread sheets.. 1 per month per year??

    I would do it this way:

    start excel.. add a workbook... delete 2 sheets (so you only have 1)

    the in you loop.. add a sheet.. fill it out.. name it month_year
    next loop add again.. etc..

    use activesheet.copyfromrecordset

    if you attach your test DB then i can play with it a bit

    you need to either zip the DB or just change the extention to txt to post it.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re:

    Hello.

    Thanks for the Welcome. I'm going to post a sample DB that has my basic setup and the code for export behind a form called 'form'. I'd like to keep everything automated in Access because each export will have to be mailed out to someone and I'd like to not have to open Excel.

    Generally the period for a group is 12 months ( for this case it would be 2006). But, some records fall just outside of that year.

    I guess my goal would be to not have to create 36 tabs for this scenario because there are technically records with 3 different years. I'd like for all months to show for the principle year 2006, and only those with records show for 2005 and 2007

    Thanks again for the help!
    Attached Files Attached Files

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    so just 3 tabs in that case?
    2005, 2006, 2007???
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Export To Excel into Wkshts by mnth and yr

    No not exactly. What I'd like is tabs as follows: dec 2005, jan 2006 feb 2006 mar 2006 april 2006...etc, jan 2007 only.

    So, Ideally, month and year as tab labels, but only including those months in 2005 and 2007 that have records.
    Thanks

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    TO make Excel not visible then comment out the visible = true line
    make sure you save the workbook in code when done...

    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:\Documents and Settings\Desktop\Excel Exports\" & [Forms]![Form]![TC] & ".xls"
    12.    
    13.     If Dir(strFilename) <> "" Then
    14.         resp = MsgBox("This group's import already exists." & vbCrLf & "Do you wish to replace it?", vbYesNo)
    15.         If resp = vbYes Then
    16.             Kill strFilename
    17.         Else
    18.             Exit Sub
    19.         End If
    20.     End If
    21.     Dim xlApp As Object
    22.     Dim xlWB As Object
    23.     Dim xlWS As Object
    24.         Set xlApp = CreateObject("Excel.Application")
    25.         xlApp.Visible = True
    26.         Set xlWB = xlApp.workbooks.Add()
    27.         xlWB.Sheets(2).Delete
    28.         xlWB.Sheets(2).Delete
    29.    
    30.     Dim rs As New Recordset
    31.     Dim cnn As Connection
    32.     Set cnn = Application.CurrentProject.Connection
    33.     YearFirst = DMin("Year(ExpDate)", "Table1")
    34.     YearLast = DMax("Year(ExpDate)", "Table1")
    35.     For Yr = YearFirst To YearLast
    36.         If DCount("Year(Expdate)", "Table1", "Year(Expdate)=" & Yr) > 0 Then
    37.             For I = 1 To 12
    38.                
    39.                
    40.                 If DCount("Month(Expdate)", "Table1", "Month(Expdate)=" & I) > 0 Then
    41.                     strSQL = "SELECT  Table1.Customer, Table1.ZipCode, Table1.ItemType, Table1.ExpDate "
    42.                     strSQL = strSQL & "FROM Table2 INNER JOIN Table1 ON Table2.GroupCode = Table1.GroupCode "
    43.                     strSQL = strSQL & "WHERE Table1.GroupCode='" & [Forms]![Form]![TC] & "' AND Month(Table1.Expdate)= " & I & " AND Year(Table1.Expdate) = " & Yr
    44.                     strSQL = strSQL & " ORDER BY Table1.Customer"
    45.                    
    46.                     rs.Open strSQL, cnn, adOpenDynamic, adLockOptimistic
    47.                     If (Yr <> Year(Date) And rs.EOF <> True) Or Yr = Year(Date) Then
    48.                        Set xlWS = xlWB.Sheets.Add(, xlWB.Sheets(xlWB.Sheets.Count))
    49.                        xlWS.Name = Format(CDate(I & "/01/" & Yr), "mmm YYYY")
    50.                        xlWS.range("A2").copyfromrecordset rs
    51.                      
    52.                     End If
    53.                     rs.Close
    54.                 End If
    55.             Next I
    56.          End If
    57.     Next Yr
    58.     'FormatWB strFilename
    59. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Export To Excel into Wkshts by mnth and yr

    This is great!! it appears to work perfectly...

    just a few questions...i'm very new to code i was wondering if you could show me how to save the workbook with code.

    Also, I have some formatting of the output that I was doing in my code above with the procedure Sub FormatWB(strFilename As String). Could I just call that procedure at the end of the code?

    Thanks again for the prompt responses. This will be a lifesaver.

    Also, could you explain how its creating the tabs it is? Is it defaulting that it should create 12 tabs for 2006? Thank you again.
    Last edited by jskillz25; Jul 12th, 2006 at 12:46 PM.
    Thanks

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    well i cut a few corners.. cant give you the WHOLE pie now can i?

    I did delete 2 sheets.. so the Workbook starts with one..
    I never removed the leftover Sheet1...(u should be able to figure that one out..tip.. you cannot delete that sheet if its the only sheet.. so do it at the end)

    well you could call the format sub.. BUT you are opening the file etc.. instead.. pass in the whole workbook object
    VB Code:
    1. Sub FormatWB(xlWB As Object)
    2. Dim xlWS As Object
    3.    Set xlWB = xlApp.Workbooks.Open(strFilename)

    so call it like this from the other sub

    FormatWB xlWB
    (in the same place you were doing it)

    Dont Close the Wkbook from the Format Sub (or the form)

    after you call the formatWB

    xlWB.SaveAs "c:\Path\To\Filename.xls"
    xlWB.Close

    then be sure to
    Set xlWS = Nothing
    Set xlWB = Nothing
    xlAPP.Quit
    Set xlApp = Nothing
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    I forgot to explain.... sorry

    alright..
    these lines create the sheet and name it
    Set xlWS = xlWB.Sheets.Add(, xlWB.Sheets(xlWB.Sheets.Count))
    xlWS.Name = Format(CDate(I & "/01/" & Yr), "mmm YYYY")

    it will ALWAYS create 12 month tabs for the Current Year
    and Only a tab for each month of prev/next year that has data...
    this is done in the IF statement

    If (Yr <> Year(Date) And rs.EOF <> True) Or Yr = Year(Date) Then

    if the Yr (Yr from loop) is Not = Year(Date) Current Year AND the Rs.EOF (EndOfFile) is Not true .. meaning it has records.. then allow it to create the sheet
    OR
    If the Yr is the Current Year (Year(Date)) then allow it

    how I explained that well enough

    Here is the BEST TIP you will get concerning working with excel
    in excel.. record a macro of what you are trying to do... then look at the macro code

    theres your code! (youwill need to tweak some things... but thats basically it)

    also.. you can add a refernce to the MS Excel x.0 Object Library in access.. then programming for excel is easier.. you will get the intellisense dropdowns
    Dim xls As Excel.Application
    xls.[DROPDOWN]
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Export To Excel into Wkshts by mnth and yr

    Thanks for the explanation Static...you've been of great help.

    I'm stumbling a bit though putting this code in action...let me show you what I currently have and see what you think.

    Its giving me an error where i call the formatWB x1WB saying byRef ArgType Mismatch.

    VB Code:
    1. Option Compare Database
    2. Private Sub Command1_Click()
    3. CreateXL
    4. End Sub
    5. Sub CreateXL()
    6. Dim strSQL As String
    7. Dim qdf As Object
    8. Dim strFilename As String
    9. Dim I As Long
    10. Dim Yr As Long
    11. Dim YearFirst As Long
    12. Dim YearLast As Long
    13. Dim resp
    14.  
    15.     strFilename = "C:\Documents and Settings\Desktop\Excel Exports\" & [Forms]![Form]![TC] & ".xls"
    16.    
    17.     If Dir(strFilename) <> "" Then
    18.         resp = MsgBox("This group's import already exists." & vbCrLf & "Do you wish to replace it?", vbYesNo)
    19.         If resp = vbYes Then
    20.             Kill strFilename
    21.         Else
    22.             Exit Sub
    23.         End If
    24.     End If
    25.     Dim xlApp As Object
    26.     Dim xlWB As Object
    27.     Dim xlWS As Object
    28.         Set xlApp = CreateObject("Excel.Application")
    29.         xlApp.Visible = True
    30.         Set xlWB = xlApp.Workbooks.Add()
    31.         xlWB.Sheets(2).Delete
    32.         xlWB.Sheets(2).Delete
    33.    
    34.     Dim rs As New Recordset
    35.     Dim cnn As Connection
    36.     Set cnn = Application.CurrentProject.Connection
    37.     YearFirst = DMin("Year(ExpDate)", "Table1")
    38.     YearLast = DMax("Year(ExpDate)", "Table1")
    39.     For Yr = YearFirst To YearLast
    40.         If DCount("Year(Expdate)", "Table1", "Year(Expdate)=" & Yr) > 0 Then
    41.             For I = 1 To 12
    42.                
    43.                
    44.                 If DCount("Month(Expdate)", "Table1", "Month(Expdate)=" & I) > 0 Then
    45.                     strSQL = "SELECT  Table1.Customer, Table1.ZipCode, Table1.ItemType, Table1.ExpDate "
    46.                     strSQL = strSQL & "FROM Table2 INNER JOIN Table1 ON Table2.GroupCode = Table1.GroupCode "
    47.                     strSQL = strSQL & "WHERE Table1.GroupCode='" & [Forms]![Form]![TC] & "' AND Month(Table1.Expdate)= " & I & " AND Year(Table1.Expdate) = " & Yr
    48.                     strSQL = strSQL & " ORDER BY Table1.Customer"
    49.                    
    50.                     rs.Open strSQL, cnn, adOpenDynamic, adLockOptimistic
    51.                     If (Yr <> Year(Date) And rs.EOF <> True) Or Yr = Year(Date) Then
    52.                        Set xlWS = xlWB.Sheets.Add(, xlWB.Sheets(xlWB.Sheets.Count))
    53.                        xlWS.Name = Format(CDate(I & "/01/" & Yr), "mmm YYYY")
    54.                        xlWS.Range("A2").copyfromrecordset rs
    55.                      
    56.                     End If
    57.                     rs.Close
    58.                 End If
    59.             Next I
    60.          End If
    61.     Next Yr
    62.     FormatWB x1WB
    63. x1WB.SaveAs "c:\Documents and Settings\Desktop\Excel Exports\" & [Forms]![Form]![TC] & ".xls"
    64. x1WB.Close
    65. Set X1WS = Nothing
    66. Set x1WB = Nothing
    67. xlApp.Quit
    68. Set xlApp = Nothing
    69. End Sub
    70. Sub FormatWB(x1WB As Object)
    71. Dim xlWS As Object
    72. Dim xlWB As Object
    73.   Setx1WB = xlApp.Workbooks.Open(strFilename)
    74.    For Each xlWS In xlWB.Worksheets
    75.        xlWS.Range("A1:L1").Font.Bold = True
    76.        xlWS.Range("A:L").Columns.AutoFit
    77.        xlWS.Range("1:1").Insert
    78.        With xlWS.Range("A1")
    79.            .Value = [Forms]![Form]![TC]
    80.            .Font.Size = 24
    81.            .Font.Bold = True
    82.        End With
    83.    Next xlWS
    84. End Sub
    Thanks

  11. #11
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    well it all looks good.. but look a little closer at your code...

    x1WB

    you have a 1 in there..

    and:
    remove the set xl and open commands...

    VB Code:
    1. Sub FormatWB(xlWB As Object)
    2. Dim xlWS As Object
    3.    For Each xlWS In xlWB.Worksheets
    4.        xlWS.Range("A1:L1").Font.Bold = True
    5.        xlWS.Range("A:L").Columns.AutoFit
    6.        xlWS.Range("1:1").Insert
    7.        With xlWS.Range("A1")
    8.            .Value = [Forms]![Form]![TC]
    9.            .Font.Size = 24
    10.            .Font.Bold = True
    11.        End With
    12.    Next xlWS
    13. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Export To Excel into Wkshts by mnth and yr

    sorry to continue to pester...but when i change to FormatWB (xWB) as my call statement it runs the procedure (opening excel and doing everything that comes before it) and then comes up with an error that says Object required and brings me to that line in the code...any ideas?
    Thanks

  13. #13
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    no pester... here.. replace all your code with this:

    then change the SaveAs Filename
    and the reason for the error was that the 1 should have been an l (lower L)


    VB Code:
    1. Option Compare Database
    2. Private Sub Command1_Click()
    3. CreateXL
    4. End Sub
    5. Sub CreateXL()
    6. Dim strSQL As String
    7. Dim qdf As Object
    8. Dim strFilename As String
    9. Dim I As Long
    10. Dim Yr As Long
    11. Dim YearFirst As Long
    12. Dim YearLast As Long
    13. Dim resp
    14.  
    15.     strFilename = "C:\Documents and Settings\Desktop\Excel Exports\" & [Forms]![Form]![TC] & ".xls"
    16.    
    17.     If Dir(strFilename) <> "" Then
    18.         resp = MsgBox("This group's import already exists." & vbCrLf & "Do you wish to replace it?", vbYesNo)
    19.         If resp = vbYes Then
    20.             Kill strFilename
    21.         Else
    22.             Exit Sub
    23.         End If
    24.     End If
    25.     Dim xlApp As Object
    26.     Dim xlWB As Object
    27.     Dim xlWS As Object
    28.         Set xlApp = CreateObject("Excel.Application")
    29.         xlApp.Visible = True
    30.         Set xlWB = xlApp.workbooks.Add()
    31.         xlWB.sheets(2).Delete
    32.         xlWB.sheets(2).Delete
    33.    
    34.     Dim rs As New Recordset
    35.     Dim cnn As Connection
    36.     Set cnn = Application.CurrentProject.Connection
    37.     YearFirst = DMin("Year(ExpDate)", "Table1")
    38.     YearLast = DMax("Year(ExpDate)", "Table1")
    39.     For Yr = YearFirst To YearLast
    40.         If DCount("Year(Expdate)", "Table1", "Year(Expdate)=" & Yr) > 0 Then
    41.             For I = 1 To 12
    42.                
    43.                
    44.                 If DCount("Month(Expdate)", "Table1", "Month(Expdate)=" & I) > 0 Then
    45.                     strSQL = "SELECT  Table1.Customer, Table1.ZipCode, Table1.ItemType, Table1.ExpDate "
    46.                     strSQL = strSQL & "FROM Table2 INNER JOIN Table1 ON Table2.GroupCode = Table1.GroupCode "
    47.                     strSQL = strSQL & "WHERE Table1.GroupCode='" & [Forms]![Form]![TC] & "' AND Month(Table1.Expdate)= " & I & " AND Year(Table1.Expdate) = " & Yr
    48.                     strSQL = strSQL & " ORDER BY Table1.Customer"
    49.                    
    50.                     rs.Open strSQL, cnn, adOpenDynamic, adLockOptimistic
    51.                     If (Yr <> Year(Date) And rs.EOF <> True) Or Yr = Year(Date) Then
    52.                        Set xlWS = xlWB.sheets.Add(, xlWB.sheets(xlWB.sheets.Count))
    53.                        xlWS.Name = Format(CDate(I & "/01/" & Yr), "mmm YYYY")
    54.                        xlWS.range("A2").copyfromrecordset rs
    55.                      
    56.                     End If
    57.                     rs.Close
    58.                 End If
    59.             Next I
    60.          End If
    61.     Next Yr
    62.     xlWB.sheets("Sheet1").Delete
    63.     FormatWB xlWB
    64.     xlWB.sheets(1).Activate
    65.     [B]xlWB.saveas "C:\temp.xls"[/B]
    66.     Set xlWS = Nothing
    67.     xlWB.Close
    68.     Set xlWB = Nothing
    69.     xlApp.Quit
    70.     Set xlApp = Nothing
    71. End Sub
    72.  
    73. Sub FormatWB(xlWB As Object)
    74. Dim xlWS As Object
    75.     For Each xlWS In xlWB.Worksheets
    76.         xlWS.range("A1:L1").Font.Bold = True
    77.         xlWS.range("A:L").Columns.AutoFit
    78.         xlWS.range("1:1").Insert
    79.         With xlWS.range("A1")
    80.             .Value = [Forms]![Form]![TC]
    81.             .Font.Size = 24
    82.             .Font.Bold = True
    83.         End With
    84.     Next xlWS
    85. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Export To Excel into Wkshts by mnth and yr

    Static you've been outstanding help.

    Just two more quick questions. I think with some of the code examples i had tried from other places online i managed to make my excel default to having 12 sheets (instead of just sheet1, sheet2, sheet3). How can I change this back?

    Also, Id like the column headings to appear above the records. I thought I had that incorporated but its not churning them out.

    Again, I'm amazed by this forums response time and accuracy.

    Thanks again
    Thanks

  15. #15
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    ahh.. ok.

    Options General - Sheets in new workbook...set it back to 3

    you had them incorporated.. BUT I forgot to put them back in.. when using CopyFromRecordset it does not output the field names...sooo
    change this part...
    (I highlight the changes)

    VB Code:
    1. If (Yr <> Year(Date) And rs.EOF <> True) Or Yr = Year(Date) Then
    2.                         Set xlWS = xlWB.sheets.Add(, xlWB.sheets(xlWB.sheets.Count))
    3.                         xlWS.Name = Format(CDate(I & "/01/" & Yr), "mmm YYYY")
    4.                         [B]For z = 0 To rs.Fields.Count - 1
    5.                             xlWS.Cells(2, z + 1) = rs.Fields(z).Name
    6.                         Next[/B]
    7.                         [B]xlWS.range("A3").copyfromrecordset rs[/B]
    8.                      
    9.                     End If
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  16. #16

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Export To Excel into Wkshts by mnth and yr

    Excellent! just one final question. I notice it's not making a tab for june 2006 ( which i believe is because there are no records for it). Is there any way to create all 12 tabs even if theres no records for the current year?

    Also, could you let me know how to make this thread resolved afterwards? (once again I'm new). I read I was supposed to do this when my questions are answered.

    THANKS
    Thanks

  17. #17
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Export To Excel into Wkshts by mnth and yr

    yep your right...
    remove the 2 higlighted lines: (or just comment them out)
    VB Code:
    1. For Yr = YearFirst To YearLast
    2.         If DCount("Year(Expdate)", "Table1", "Year(Expdate)=" & Yr) > 0 Then
    3.             For I = 1 To 12
    4.                
    5.                
    6.                 [B]If DCount("Month(Expdate)", "Table1", "Month(Expdate)=" & I) > 0 Then[/B]
    7.                     strSQL = "SELECT  Table1.Customer, Table1.ZipCode, Table1.ItemType, Table1.ExpDate "
    8.                     strSQL = strSQL & "FROM Table2 INNER JOIN Table1 ON Table2.GroupCode = Table1.GroupCode "
    9.                     strSQL = strSQL & "WHERE Table1.GroupCode='" & [Forms]![Form]![TC] & "' AND Month(Table1.Expdate)= " & I & " AND Year(Table1.Expdate) = " & Yr
    10.                     strSQL = strSQL & " ORDER BY Table1.Customer"
    11.                    
    12.                     rs.Open strSQL, cnn, adOpenDynamic, adLockOptimistic
    13.                     If (Yr <> Year(Date) And rs.EOF <> True) Or Yr = Year(Date) Then
    14.                        Set xlWS = xlWB.sheets.Add(, xlWB.sheets(xlWB.sheets.Count))
    15.                        xlWS.Name = Format(CDate(I & "/01/" & Yr), "mmm YYYY")
    16.                        xlWS.range("A2").copyfromrecordset rs
    17.                      
    18.                     End If
    19.                     rs.Close
    20.                 [B]End If[/B]
    21.             Next I
    22.          End If
    23.     Next Yr

    and to mark the thread resolved... just above your first post in this thread...
    click Thread Tools > Mark Thread Resolved

    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  18. #18

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: [RESOLVED] Export To Excel into Wkshts by mnth and yr

    Thanks again for all the help static...I'll let you know if I have any problems moving this coding over to my real database.
    Thanks

  19. #19

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: [RESOLVED] Export To Excel into Wkshts by mnth and yr

    Sorry but I'm not 100% resolved. I spoke too soon. I have a field that is a currency that is not keeping its formatting when it comes over to excel (it's coming as a number instead of currency). Is there any way I can fix this? Thanks, I hope someone sees the new post even though it reads resolved.



    I Think I found a quick and easy solution. I added this line to the format WB procedure.

    xlWS.Range("E:G").NumberFormat = "$#,##0"

    That makes those 3 columns currencies like I want them.

    Thanks again static for the great help.
    Last edited by jskillz25; Jul 13th, 2006 at 08:08 AM.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width