将 Excel 个单元格粘贴到 Power Point table 个单元格时,是什么导致随机遗漏?
What is causing random misses when pasting Excel cells to Power Point table cells?
每个月,我 运行 一个 VBA 程序用新数据(我从 Excel 文件)。我使用以下 VBA 函数来粘贴数据。
Function PastePcts(ByVal tbl As PowerPoint.Table,
ByVal oldRow As Long, ByVal oldCol As Long,
ByVal newRow As Integer, ByVal newCol As Integer)
'
' This function basically pastes data from Cell (oldRow, oldCol) of the current Excel
' spreadsheet to Cell (newRow, newCol) of the input PowerPoint table
'
Cells(oldRow, oldCol).Copy
With tbl.Cell(newRow, newCol).Shape.TextFrame.TextRange
.Paste 'Pastes new percent
Call .Replace("%","") 'Removes % sign
.Font.Bold = True 'Restores boldface
End With
End Function
PowerPoint table 上的大多数单元格都按预期更改,但其中一些随机保留了上个月的值,就像从未对它们调用此函数一样。如果我再次 运行 程序,我可能会遇到同样的问题,但是保持旧值的 table 单元格集会有所不同。
在我上次 运行 中,table 有 88 个单元格的值与上个月相比发生了变化,其中 78 个更改成功。我该如何修改代码来解决这个问题?
尝试只设置 powerpoint 单元格的值,而不是复制整个 Excel 单元格。使用剪贴板通常比处理 strings/numerical 值更慢并且不如处理可靠。特别是在不同的应用程序之间(即使是同一应用程序,Office 也不能在实例之间使用自己的剪贴板。)
With tbl.Cell(newRow, newCol).Shape.TextFrame.TextRange
.Text = Cells(oldRow, oldCol).Value
Call .Replace("%","") 'Removes % sign
.Font.Bold = True 'probably not necessary any more
End With
根据 excel 单元格的格式,您可能需要进行一些调整
我觉得这像是一个时间问题。您能否通过设置断点并单步执行所有 88 个单元格的代码来确认是否所有单元格都已填充?我在 PowerPoint 中遇到过 Copy/Paste 大量循环的类似问题,即使我尝试了以下方法也从未找到解决方案:
- 在 .Copy 方法之后添加 DoEvents 以尝试为剪贴板填充提供 OS 时间。
- 将 .Paste 方法放入 Do...While 循环并检查 Err=0 在继续之前。
- 通过简单的 WinAPI Sleep 调用减慢速度。
- 在执行 .Paste 方法检查有效内容之前询问剪贴板数据类型(更复杂的 WinAPI 调用)。
也许上述方法之一适用于您的情况。但是,当它在我的代码中随机失败时,我会收到类似 "data cannot be pasted here" 的错误,因此除非您已从发布的代码中删除错误处理,否则原因可能完全不同。
我认为 VBA 和底层 OS 之间关于这种随机行为的交互存在根本性错误...
每个月,我 运行 一个 VBA 程序用新数据(我从 Excel 文件)。我使用以下 VBA 函数来粘贴数据。
Function PastePcts(ByVal tbl As PowerPoint.Table,
ByVal oldRow As Long, ByVal oldCol As Long,
ByVal newRow As Integer, ByVal newCol As Integer)
'
' This function basically pastes data from Cell (oldRow, oldCol) of the current Excel
' spreadsheet to Cell (newRow, newCol) of the input PowerPoint table
'
Cells(oldRow, oldCol).Copy
With tbl.Cell(newRow, newCol).Shape.TextFrame.TextRange
.Paste 'Pastes new percent
Call .Replace("%","") 'Removes % sign
.Font.Bold = True 'Restores boldface
End With
End Function
PowerPoint table 上的大多数单元格都按预期更改,但其中一些随机保留了上个月的值,就像从未对它们调用此函数一样。如果我再次 运行 程序,我可能会遇到同样的问题,但是保持旧值的 table 单元格集会有所不同。
在我上次 运行 中,table 有 88 个单元格的值与上个月相比发生了变化,其中 78 个更改成功。我该如何修改代码来解决这个问题?
尝试只设置 powerpoint 单元格的值,而不是复制整个 Excel 单元格。使用剪贴板通常比处理 strings/numerical 值更慢并且不如处理可靠。特别是在不同的应用程序之间(即使是同一应用程序,Office 也不能在实例之间使用自己的剪贴板。)
With tbl.Cell(newRow, newCol).Shape.TextFrame.TextRange
.Text = Cells(oldRow, oldCol).Value
Call .Replace("%","") 'Removes % sign
.Font.Bold = True 'probably not necessary any more
End With
根据 excel 单元格的格式,您可能需要进行一些调整
我觉得这像是一个时间问题。您能否通过设置断点并单步执行所有 88 个单元格的代码来确认是否所有单元格都已填充?我在 PowerPoint 中遇到过 Copy/Paste 大量循环的类似问题,即使我尝试了以下方法也从未找到解决方案:
- 在 .Copy 方法之后添加 DoEvents 以尝试为剪贴板填充提供 OS 时间。
- 将 .Paste 方法放入 Do...While 循环并检查 Err=0 在继续之前。
- 通过简单的 WinAPI Sleep 调用减慢速度。
- 在执行 .Paste 方法检查有效内容之前询问剪贴板数据类型(更复杂的 WinAPI 调用)。
也许上述方法之一适用于您的情况。但是,当它在我的代码中随机失败时,我会收到类似 "data cannot be pasted here" 的错误,因此除非您已从发布的代码中删除错误处理,否则原因可能完全不同。
我认为 VBA 和底层 OS 之间关于这种随机行为的交互存在根本性错误...