Share via

populating excel template from access form

Anonymous
2015-06-18T16:09:56+00:00

I have a form that uses a subform to show some data. the data in the subform may vary from 1 to 48 items.  I am using the code below to populate an excel template but don't know how to get items into a specific range (k21-K52) on the spreadsheet from the subform.  The subform is a data sheet so the number of items can vary.  Since the range on the specific sheet is only 32 anything over 32 will need to be put on the second sheet.

Any help or suggestions would be greatly appreciated.

Thanks

Bob

' Microsoft Excel 11.0 Object Library

    Dim appExcel As Excel.Application

    Dim wbook As Excel.Workbook

    Dim wsheet As Excel.Worksheet

    Set appExcel = New Excel.Application

   ' appExcel.Visible = True

    Set wbook = appExcel.Workbooks.Open("C:\Users\Desktop\Work_Order_Request.xls")

    Set wsheet = wbook.Worksheets("YF-MFR-02-2")

    With wsheet

        .Range("F6").Value = WOSubmittedDate

   End With

    Set wsheet = Nothing

    ' Saves as new name

    fName = "C:\Users\Desktop\testing.xls"

    wbook.SaveAs FileName:=fName

    wbook.Close True

    Set wbook = Nothing

    Set appExcel = Nothing

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2015-06-25T19:09:17+00:00

    I was able to figure out what I needed but now am having problems with one section of code. The problem is when the code changes to another sheet to continue adding the data. I have noticed that it is putting each record 9 times.  Not sure why this is happening. Any  suggestions would be appreciated.

    Thanks

    Bob

                Dim xlRow As Integer

                Dim lRow As Integer

                Dim rs As DAO.Recordset

                Dim c As Integer

                Set rs = Me.Child207.Form.RecordsetClone

                xlRow = (wsheet.Columns("K").End(xlDown).Row)

                c = 11

                xlRow = xlRow + 16

             Do Until rs.EOF

            '     For Each fld In rs.Fields

                 For Each MfrPartNumber In rs.Fields

                    If wsheet.Name = "MFR-02-2" Then

                 wsheet.Cells(xlRow, c).Value = rs!MfrPartNumber

                 wsheet.Cells(xlRow, 14).Value = rs!PartRev

                 wsheet.Cells(xlRow, 15).Value = rs!Quantity

                   lRow = 4

                   ElseIf wsheet.Name = "MFR-02-2A-EPL" Then

                              wsheet.Cells(lRow, 1).Value = rs!MfrPartNumber

                              wsheet.Cells(lRow, 5).Value = rs!PartRev

                              wsheet.Cells(lRow, 6).Value = rs!Quantity

                        lRow = lRow + 1

                    End If

                 Next MfrPartNumber

                 xlRow = xlRow + 1

                 rs.MoveNext

                If xlRow > 24 Then

                  Set wsheet = wbook.Worksheets("MFR-02-2A-EPL")

               End If

               Loop

            Set rs = Nothing

    Was this answer helpful?

    0 comments No comments