VBA-Excel公式参考 - 计算刷新

时间:2016-08-29 19:05:54

标签: excel vba excel-vba

这是我project.json documentation的后续跟进。我成功地能够在不同的驱动器上打开不同的工作簿,将数据作为图片复制到一个范围内,然后将其粘贴到ThisWorkbook中。我现在遇到的问题是{{1}我正在使用的是在计算时捕获单元格值,因此最终看起来像一堆.CopyPicture值。

我已经使用了一些不同的东西来看看我是否可以在复制它们之前得到公式来计算,但看起来电子表格在宏不再运行之前不会完成计算。

我检查了previous post,但我不完全确定如何实施#N/A Requesting Data...。对此有何帮助?

原始代码:

if Application.CalculationState is xLdone then loop else wait

首次尝试:

Dim BBPic As Workbook
Dim test As Workbook
Set BBPic = Application.Workbooks.Open("\\OtherDrive\Shared\OtherGroup\DailySheet.xlsx")
Set test = ThisWorkbook

BBPic.Sheets("Sheet1").Range("B2:E16").CopyPicture
test.Sheets("Summary").Range("B64").PasteSpecial

第二次尝试:

Dim BBPic As Workbook
Dim test As Workbook
Set BBPic = Application.Workbooks.Open("\\OtherDrive\Shared\OtherGroup\DailySheet.xlsx")
Set test = ThisWorkbook

BBPic.Sheets("Sheet1").Range("B2:E16").CopyPicture
Application.Wait (Now + TimeValue("0:01:00"))
test.Sheets("Summary").Range("B64").PasteSpecial
Workbooks("DailySheet.xlsx").Close SaveChanges:=False

最后的尝试:

Dim BBPic As Workbook
Dim test As Workbook
Set BBPic = Application.Workbooks.Open("\\OtherDrive\Shared\OtherGroup\DailySheet.xlsx")
Set test = ThisWorkbook

BBPic.Sheets("Sheet1").Range("B2:E16").CopyPicture
ActiveWorkbook.RefreshAll
test.Sheets("Summary").Range("B64").PasteSpecial
Workbooks("DailySheet.xlsx").Close SaveChanges:=False

编辑:使用Dim BBPic As Workbook Dim test As Workbook Set BBPic = Application.Workbooks.Open("\\OtherDrive\Shared\OtherGroup\DailySheet.xlsx") Set test = ThisWorkbook BBPic.Sheets("Sheet1").Range("B2:E16").CopyPicture ActiveSheet.Calculate test.Sheets("Summary").Range("B64").PasteSpecial Workbooks("DailySheet.xlsx").Close SaveChanges:=False

进行第4次尝试
Application.CalculationState = xlDone

1 个答案:

答案 0 :(得分:1)

我将我的宏分成两部分,利用Application.RunApplication.OnTime Now + TimeValue("00:00:05")感谢this post和@cyboashu通知我。我所经历的是真实的:Bloomberg数据不会刷新,除非宏已经结束,所以你必须将它分解成2个宏,第一个刷新数据,第二个执行你想做的事情。

Sub OpenDailySheet()
'
'Macro
'

'

Dim BBPic As Workbook
Set BBPic = Application.Workbooks.Open("\\OtherDrive\Shared\OtherGroup\DailySheet.xlsx")
Application.Run "RefreshAllStaticData"
Application.OnTime Now + TimeValue("00:00:05"), "PasteChart"


End Sub

Sub PasteChart()

Dim test As Workbook
Set test = ThisWorkbook

Workbooks("DailySheet.xlsx").Sheets("Sheet1").Range("B2:E16").CopyPicture
test.Sheets("Summary").Range("B64").PasteSpecial
Workbooks("DailySheet.xlsx").Close SaveChanges:=False

End Sub