一个打开的工作簿看不到其他打开的工作簿
我们有这款老式软件,可以打开新的 Excel 工作簿并在其中填充数据。它不再做任何事情。工作簿保持未保存状态并打开。
现在我创建了一个宏,它按名称查找这个新打开的工作簿(使用 Workbooks.Count
和 Application.Workbooks(i).Name
),当找到它时,它从中复制一些数据。
不幸的是,自从升级到 Office 365 后,Excel 看不到未保存的工作簿,因此宏停止工作。未保存的工作簿甚至不计入 Workbooks.Count
中。
有什么办法可以让我在最新版本的 Excel 中再次执行此操作吗?
编辑:不幸的是我无法编辑oldschool程序的代码。令我困扰的是,当我使用 2013 excel 时,它运行得非常完美。
Sub zkopirujPoctyZeSesitu()
rozdilPoctuZbozi = 0
rozdilUZbozi = 0
Dim aktualne As Integer
Dim bylo As Integer
Dim franta As String
idSesit = -1
Dim hledas As String
'nalezeni ID Sešitu z NAV
For i = 1 To Workbooks.Count
If InStr(Application.Workbooks(i).Name, "Sešit") > 0 Then
idSesit = i
ElseIf InStr(Application.Workbooks(i).Name, "GENERATOR") > 0 Then
idGenerator = i
End If
Next i
'kontrola zda je otevřenej stav skladu
If idSesit = -1 Then
MsgBox ("Nelze načíst stav skladu z NAV!!!!")
End
ElseIf Not (Workbooks(idSesit).Worksheets(1).Cells(1, 1).Text = "Číslo zboží" And Workbooks(idSesit).Worksheets(1).Cells(1, 2).Text = "Varianta zboží") Then
MsgBox ("Je třeba zavřít všechny Excel soubory s názvem" & Chr(34) & "Sešit" & Chr(34) & vbNewLine & "(kromě výstupního DatSkladu z NAV)")
End
End If
For i = 1 To List1.Cells(Rows.Count, 1).End(xlUp).Row
If Len(List1.Cells(i, 1).Text) = 5 And Left$(List1.Cells(i, 1).Text, 1) = "0" Then
hledas = Right(List1.Cells(i, 1).Text, Len(List1.Cells(i, 1).Text) - 1)
Else
hledas = List1.Cells(i, 1).Value
End If
Set FoundCell = Workbooks(idSesit).Worksheets(1).Range("A:A").Find(What:=hledas, After:=Workbooks(idSesit).Worksheets(1).Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not FoundCell Is Nothing Then
'ochrana proti Mertens lagerSchuette
If (Workbooks(idSesit).Worksheets(1).Cells(FoundCell.Row, 6).Value < 0) Then
MsgBox ("Stav skladu je z Mertensu!!!!")
End
End If
'o kolik se lisi stav
aktualne = Workbooks(idSesit).Worksheets(1).Cells(FoundCell.Row, 6).Value
bylo = List1.Cells(i, 20).Value
rozdilPoctuZbozi = rozdilPoctuZbozi + aktualne - bylo
'pocet zbozi u kteryho je rozdilny stav
If aktualne <> bylo Then
rozdilUZbozi = rozdilUZbozi + 1
End If
'prepis poctu ks
List1.Cells(i, 20).Value = Workbooks(idSesit).Worksheets(1).Cells(FoundCell.Row, 6).Value
End If
Next i
结束子
We have this oldschool software which opens new Excel workbook and fills it with data. It does not do anything more. The workbook stays unsaved and opened.
Now I have created a macro which looks for this newly opened workbook by name (using Workbooks.Count
and Application.Workbooks(i).Name
) and when it finds it, it copies some data from that.
Unfortunately since upgrading to Office 365, the Excel does not see the unsaved workbook so the macro stopped working. The unsaved workbook is not even counted in Workbooks.Count
.
Is there any way for me to make this work again in newest version of Excel?
EDIT: Unfortunately I cannot edit the code of the oldschool program. What bothers me, that when I was using 2013 excel, it worked flawlessly.
Sub zkopirujPoctyZeSesitu()
rozdilPoctuZbozi = 0
rozdilUZbozi = 0
Dim aktualne As Integer
Dim bylo As Integer
Dim franta As String
idSesit = -1
Dim hledas As String
'nalezeni ID Sešitu z NAV
For i = 1 To Workbooks.Count
If InStr(Application.Workbooks(i).Name, "Sešit") > 0 Then
idSesit = i
ElseIf InStr(Application.Workbooks(i).Name, "GENERATOR") > 0 Then
idGenerator = i
End If
Next i
'kontrola zda je otevřenej stav skladu
If idSesit = -1 Then
MsgBox ("Nelze načíst stav skladu z NAV!!!!")
End
ElseIf Not (Workbooks(idSesit).Worksheets(1).Cells(1, 1).Text = "Číslo zboží" And Workbooks(idSesit).Worksheets(1).Cells(1, 2).Text = "Varianta zboží") Then
MsgBox ("Je třeba zavřít všechny Excel soubory s názvem" & Chr(34) & "Sešit" & Chr(34) & vbNewLine & "(kromě výstupního DatSkladu z NAV)")
End
End If
For i = 1 To List1.Cells(Rows.Count, 1).End(xlUp).Row
If Len(List1.Cells(i, 1).Text) = 5 And Left$(List1.Cells(i, 1).Text, 1) = "0" Then
hledas = Right(List1.Cells(i, 1).Text, Len(List1.Cells(i, 1).Text) - 1)
Else
hledas = List1.Cells(i, 1).Value
End If
Set FoundCell = Workbooks(idSesit).Worksheets(1).Range("A:A").Find(What:=hledas, After:=Workbooks(idSesit).Worksheets(1).Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not FoundCell Is Nothing Then
'ochrana proti Mertens lagerSchuette
If (Workbooks(idSesit).Worksheets(1).Cells(FoundCell.Row, 6).Value < 0) Then
MsgBox ("Stav skladu je z Mertensu!!!!")
End
End If
'o kolik se lisi stav
aktualne = Workbooks(idSesit).Worksheets(1).Cells(FoundCell.Row, 6).Value
bylo = List1.Cells(i, 20).Value
rozdilPoctuZbozi = rozdilPoctuZbozi + aktualne - bylo
'pocet zbozi u kteryho je rozdilny stav
If aktualne <> bylo Then
rozdilUZbozi = rozdilUZbozi + 1
End If
'prepis poctu ks
List1.Cells(i, 20).Value = Workbooks(idSesit).Worksheets(1).Cells(FoundCell.Row, 6).Value
End If
Next i
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要检查内存中是否有另一个Excel实例正在运行,即是否有另一个
excel.exe
进程正在运行。如果是这样,您可以考虑连接到正在运行的 Excel 实例并从那里获取Workbooks
集合。但是,您需要注意两个进程可以在不同的安全上下文下运行,在这种情况下,无法从另一个进程访问另一个进程。您可能会发现 GetObject 和 CreateObject 行为办公自动化服务器文章很有帮助。
First of all, you need to check whether another Excel instance is running in memory, i.e. whether another
excel.exe
process is running. If so, you may consider connecting to the running Excel instance and get theWorkbooks
collection from there. However, you need to be aware that two processes can be run under different security contexts, in that case there is no way to reach one from another.You may find the GetObject and CreateObject behavior of Office automation servers article helpful.