Results 1 to 2 of 2

Thread: Export Access MSFlexgrid to Excel

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76

    Export Access MSFlexgrid to Excel

    I have an MSFlexgrid on a form in an Access XP database. I would like to export the data in the grid to Excel. I know how to do this in VB, but I am not sure if the same code can be used in Access. When I try it, it gives me run-time error 381 - Subscript out of range

    VB Code:
    1. Private Sub cmdExport_Click()
    2.     On Error Resume Next
    3.     Dim xLApp As Excel.Application
    4.     Dim xLWB As Excel.Workbook
    5.     Dim xLSH As Excel.Worksheet
    6.     Dim RowCounter As Integer
    7.     Dim ColCounter As Integer
    8.    
    9.      'open Excel application
    10.     Set xLApp = CreateObject("Excel.application")
    11.     'Create Excel workbook
    12.     xLApp.Workbooks.Add
    13.     Set xLWB = xLApp.Workbooks(1)
    14.  
    15.     'Using Sheet 1
    16.     Set xLSH = xLWB.Worksheets(1)
    17.     For RowCounter = 0 To 44
    18.         For ColCounter = 0 To 1
    19.         With grdPlans
    20.              xLSH.Cells((StartRow + RowCounter) - 1, (StartCol + ColCounter) - 1).Value = _
    21.             .TextMatrix(RowCounter - 1, ColCounter - 1) & " "
    22.         End With
    23.         Next
    24.     Next
    25.    
    26.     xLWB.SaveAs (App.Path & "\Plan.xls")
    27.    
    28.     xLWB.Close
    29.     xLApp.Quit
    30.    
    31.     Set xLWB = Nothing
    32.     Set xLApp = Nothing
    33. End Sub

    Is there a way to do this?

  2. #2
    Member
    Join Date
    Mar 2002
    Location
    Grimsby (up the Mariners!)
    Posts
    45

    Check your subscript values

    What are the values of and StartRow and StartCol? If they are 0 or 1 when your for loops start you will end up with:

    Cells 'row' subscript = 0 + 0 - 1 = -1
    Cells 'row' subscript = 1 + 0 - 1 = 0

    Both 0 and 1 are invalid in the .Cells(Row,Col) property.
    Cells(1,1) is cell "A1".

    Also check the starting subscript values you are passing to the TextMatrix of the flexgrid.

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