【发布时间】:2017-05-28 05:04:51
【问题描述】:
我有这个代码。我基本上是在一张纸上生成一个列表并将其重命名为 RSSR 列表。然后我拿那张纸并将其移动到现有的纸上。发生的情况是最后几行代码没有保存我在其上进行所有格式化的工作簿,并且 excel 没有关闭。我将工作表移动到保存的工作簿并且该 excel 实例已关闭。当我在 excel 上结束任务并重新运行代码时,它说实例不再存在,例如服务器或机器不再存在。我无法获取要保存并关闭 excel 实例的 excel 工作表。如果它杀死了 excel,它会在我下次运行该程序时出错。我希望在此过程中关闭 excel。这是我的代码:
Public Function BrooksFormatBrooks()
Dim xlApp As Excel.Application
Dim xlApp2 As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim wb2 As Excel.Workbook
Dim ws2 As Excel.Worksheet
Dim MyFileName As String
Dim afile As String
Dim bfile As String
afile = "S:\Brooks\Tyco-Brooks Receiving Tracking MASTER V 1.4 2017-05-06.xlsx"
bfile = "S:\_Reports\Brooks\Tyco-Brooks Receiving Tracking MASTER - "
MyFileName = bfile & Format(Date, "mm-dd-yyyy") & ".xls"
MyFileName2 = afile
On Error Resume Next
Set xlApp = CreateObject("Excel.Application")
On Error GoTo 0
Set wb2 = xlApp2.Workbooks.Open(MyFileName2)
Set ws2 = wb2.Sheets(1)
ws2.Activate
xlApp.DisplayAlerts = False
wb2.Sheets("RSSR_List").Delete
xlApp.DisplayAlerts = True
wb2.CheckCompatibility = False
wb2.Save
wb2.CheckCompatibility = True
wb2.Close SaveChanges:=False
xlApp.Quit
Set xlApp = Nothing
Set wb2 = Nothing
Set ws2 = Nothing
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
wb.ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$312"), , xlYes).Name = _
"RSSR"
ws.Range("A1:F312").Select
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:=False
Set wb2 = xlApp.Workbooks.Open(MyFileName2)
MsgBox "Before Move"
ws.Move Before:=Workbooks("Tyco-Brooks Receiving Tracking MASTER V 1.4 2017-05-06.xlsx").Sheets(1)
MsgBox "AFter Move"
wb2.CheckCompatibility = False
wb2.Save
wb2.CheckCompatibility = True
wb2.Close SaveChanges:=True
Set wb = xlApp.Workbooks.Open(MyFileName)
wb.CheckCompatibility = False
wb.Save
wb.CheckCompatibility = True
wb.Close SaveChanges:=True
xlApp.Quit
Set xlApp = Nothing
Set wb = Nothing
Set ws = Nothing
Set wb2 = Nothing
Set ws2 = Nothing
End Function
【问题讨论】:
-
Dim xlApp2 As Excel.Application然后Set wb2 = xlApp2.Workbooks.Open(MyFileName2)你在这里使用了一个未初始化的变量(xlApp2),它是怎么通过的?您是否发布了您的确切代码?除了你为什么想要两个Excel.Application对象? -
这是 Excel VBA 代码吗?如果是这样,您为什么需要 任何 个额外的 Excel 应用程序对象? (或者这只是使用 Excel 的 MSAccess 或 MSWord [等] 代码?)
-
(a) 你有一些不合格的引用 -
Range("$A$1:$F$312")应该是ws.Range("$A$1:$F$312")而不是默认为Application.ActiveWorkbook.ActiveSheet.Range("$A$1:$F$312"),Before:=Workbooks("Tyco-Brooks Receiving Tracking MASTER V 1.4 2017-05-06.xlsx").Sheets(1)应该是Before:=xlApp.Workbooks("Tyco-Brooks Receiving Tracking MASTER V 1.4 2017-05-06.xlsx").Sheets(1)(b) 这可能很危险在工作表所在的工作簿关闭后移动工作表。 -
@YowE3K
Set wb2 = xlApp2...xlApp2未初始化怎么办?我错过了什么吗? -
@A.S.H - 不,这肯定很糟糕(尤其是当 OP 在删除存在于
xlApp2中的工作簿工作表之前使用xlApp.DisplayAlerts时),但你已经提到过我懒得重复了。我怀疑有人在玩代码,发布的版本不是用于产生问题中描述的症状的版本。