具有访问权限的excel vba不会关闭此代码

时间:2017-05-27 21:26:59

标签: excel vba ms-access

嗨我几分钟前发布了一篇文章,并且对我的问题表示不满意。我正在使用访问权限打开工作表并添加表格。 Excel将不会关闭导致问题,因为当我在另一个函数中再次获取excel对象时,我正在使用的工作表将无法打开并且不会格式化它。这是我的代码。我以为我在这里是明确的,但也许我不是。 Excel不会关闭。

Public Function BrooksFormatTableBrooks()
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

bfile = "S:\_Reports\Brooks\Tyco-Brooks Receiving Tracking MASTER - "

MyFileName = bfile & Format(Date, "mm-dd-yyyy") & ".xls"

On Error Resume Next
Set xlApp = CreateObject("Excel.Application")
On Error GoTo 0

Set wb = xlApp.Workbooks.Open(MyFileName)
Set ws = wb.Sheets(1)
ws.Activate

wb.Sheets(1).Name = "RSSR_List"

Set ws = wb.Sheets(1)
ws.Activate

xlApp.ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$312"), , xlYes).Name = _
     "RSSR"

ws.Range("A1:F312").Select
DoEvents

ws.Cells.Rows("2:2").Select
xlApp.ActiveWindow.FreezePanes = False
xlApp.ActiveWindow.FreezePanes = True

ws.Columns("A:Z").HorizontalAlignment = xlCenter
ws.Rows("1:1").Font.Bold = True
ws.Rows("1:1").Font.ColorIndex = 1
ws.Rows("1:1").Interior.ColorIndex = 15
ws.Cells.Font.Name = "Calbri"
ws.Cells.Font.Size = 8
ws.Cells.EntireColumn.AutoFit
ws.Cells.EntireRow.AutoFit

xlApp.Cells.Borders.LineStyle = xlContinuous
xlApp.Cells.Borders.Weight = xlThin
xlApp.Cells.Borders.ColorIndex = 0

ws.Cells.Rows("1:1").Select

wb.CheckCompatibility = False
wb.Save
wb.CheckCompatibility = True
wb.Close SaveChanges:=True

xlApp.Quit

Set xlApp = Nothing
Set wb = Nothing
Set ws = Nothing
MsgBox "Table Add"
End Function

1 个答案:

答案 0 :(得分:2)

Range("$A$1:$F$312")替换为ws.Range("$A$1:$F$312"),否则您仍然会引用一个Excel应用程序对象,该对象在您退出MSAccess之前不会被销毁。