• Excel won’t close (2K)

    Author
    Topic
    #382956

    I’m running this code from Access to format some cells in an exported spreadsheet.
    Dim oXL As New Excel.Application
    Dim oXB As New Excel.Workbook

    oXL.Workbooks.Open (strFILE)
    Set oXB = oXL.Workbooks.Open(strFILE)

    With oXL.Application
    .Columns(“J:J”).Select
    .Selection.NumberFormat = “@” ‘formats as text
    .Range(“A2”).Select
    .Range(Selection, Selection.End(xlDown)).Select
    .Selection.NumberFormat = “0” ‘formats as number
    .Cells.Select
    .Selection.Columns.AutoFit
    .Range(“A1”).Select

    End With
    oXB.Save
    Set oXB = Nothing
    oXL.Quit
    Set oXL = Nothing
    If I Ctrl-Alt-Del and go to the task list I still see Excel running in the background.
    Am I not closing it properly in the code?
    Thanks

    Viewing 0 reply threads
    Author
    Replies
    • #651301

      Declare oXB without the New keyword:

      Dim oXB As Excel.Workbook

      I suspect that using New causes a second instance of Excel to be created.

      Other remarks:

      Omit the line

      oXL.Workbooks.Open (strFILE)

      It is superfluous, since you open the file again in the next line.

      You don’t need to select ranges in order to format them:

      With oXL.Application
      .Columns(“J:J”).NumberFormat = “@” ‘formats as text
      .Range(.Range(“A2”), .Range(“A2”).End(xlDown)).NumberFormat = “0” ‘formats as number
      .Cells.Columns.AutoFit
      End With

    Viewing 0 reply threads
    Reply To: Excel won’t close (2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: