【问题标题】:vba excel stays open when moving an excel sheet移动excel工作表时vba excel保持打开状态
【发布时间】: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 时),但你已经提到过我懒得重复了。我怀疑有人在玩代码,发布的版本不是用于产生问题中描述的症状的版本。

标签: excel vba


【解决方案1】:

有时这些问题可以通过在有问题的操作之后调用DoEvents 来解决。所以在这种情况下,你会有类似的东西:

MsgBox "Before Move"
ws.Move Before:=Workbooks("Tyco-Brooks Receiving Tracking MASTER V 1.4 2017-05-06.xlsx").Sheets(1)
DoEvents
MsgBox "AFter Move"

这对于 Excel 2016 来说往往是必需的。

【讨论】:

  • 我做了以下
  • ws.move Before:=xlApp.Workbooks(..... 并在其下方添加了 doevents 并且现在关闭了 excel 就好了 谢谢谢谢谢谢我已经打了几个小时的头现在。
【解决方案2】:
ws.Move Before:=xlApp.Workbooks("Tyco-Brooks Receiving Tracking Master V 1.4...)Sheets(1)
DoEvents

这行得通。

【讨论】:

  • 您的代码是如何通过使用未初始化的xlApp2 的? (并且您的答案中的 DoEvents 不是必需的。或者,至少,它不应该是必需的。)
  • DoEvents 在 Excel 2016 中不应该是必需的,但遗憾的是它有时是必需的。您必须像糖果一样将它们撒在周围,才能让事情像在 Excel 2013 中那样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多