Insert Rows into an Excel Spreadsheet from Access using VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davnao
    New Member
    • May 2007
    • 8

    Insert Rows into an Excel Spreadsheet from Access using VB

    I would like to open an existing workbook, select a work sheet and insert multiple rows at the top of the spreadsheet. Have the following code, but unsure of proper command & syntax for Insert row, and how to make it insert at row A1.

    Code:
        With DataSheet
            .Sheets(2).Select
            .Rows.Insert
            .Cells(1, 1).Value = txtLine1                                 
            .Cells(2, 1).Value = txtLine2
            .Cells(3, 1).Value = txtLine3
            .Cells(4, 1).Value = txtLine4
        End With
    Last edited by NeoPa; May 24 '07, 11:34 PM. Reason: Tags
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    Assuming you have a variable lngXtra holding the number of extra rows required above A1.
    Code:
    With DataSheet
        .Sheets(2).Select
        Call .Range("A1:A" & lngXtra).EntireRow.Insert
        .Cells(1, 1).Value = txtLine1                                 
        .Cells(2, 1).Value = txtLine2
        .Cells(3, 1).Value = txtLine3
        .Cells(4, 1).Value = txtLine4
    End With

    Comment

    • davnao
      New Member
      • May 2007
      • 8

      #3
      Originally posted by NeoPa
      Assuming you have a variable lngXtra holding the number of extra rows required above A1.
      Code:
      With DataSheet
          .Sheets(2).Select
          Call .Range("A1:A" & lngXtra).EntireRow.Insert
          .Cells(1, 1).Value = txtLine1                                 
          .Cells(2, 1).Value = txtLine2
          .Cells(3, 1).Value = txtLine3
          .Cells(4, 1).Value = txtLine4
      End With
      Works like a charm! Thanks!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        No problem.
        I actually do a large amount of work in Excel too so that was an easy one ;)

        Comment

        Working...