【问题标题】:Alternative methods for coverting formulas to values in Excel using VBA使用 VBA 将公式转换为 Excel 中的值的替代方法
【发布时间】:2015-03-16 18:46:37
【问题描述】:

我想创建一个流程,首先从 Bloomberg 中提取数据,然后用它们的值替换所有公式。到目前为止,我提出了两种方法。 1. 使用 BDH 代码首先为一系列单元格提取数据,然后在填充所有数据后将所有内容作为值粘贴。 2. 使用 BDH 代码立即提取数据并将其粘贴为值,然后代码才能执行下一个单元格。以下是我的代码(我使用了第二种方法):

For j = lastrow - 6 To lastrow
time = ActiveSheet.Cells(j, 1)
    For i = 3 To lastcol 'starts from Col C
    ticker = ActiveSheet.Cells(2, i).Value
    ActiveSheet.Cells(j, i).Formula = "= BDH(""" & ticker & """,""Last_Price"",""" & time & """,""" & time & """, ""days=a"", ""fill=p"")"
    Application.Wait (Now + TimeValue("00:00:03"))
    ActiveSheet.Cells(j, i).Value = ActiveSheet.Cells(j, i).Value
    Next
Next

当我在 Bloomberg 上运行此代码时,它不会粘贴为值,它们仍然是代码。我觉得 Application.Wait 中的 3 秒不够长,但是我想改变我的整个方法并且不想计算填充一个值的秒数。有没有人有这方面的经验并且可以提供一种更快的方法来填充彭博的值?虽然我上面提到的第一种方法也是一种替代方法,但我更喜欢在填充数据时获取值。谢谢!

【问题讨论】:

    标签: excel vba bloomberg


    【解决方案1】:

    我实际上想出了一个答案来解决我自己的问题..但它仍然使用 OnTime..不确定是否有其他方法可以做到这一点

    Sub pulldata()
        For j = lastrow - 6 To lastrow
        time = ActiveSheet.Cells(j, 1)
            For i = 3 To lastcol 'starts from Col C
            ticker = ActiveSheet.Cells(2, i).Value
            ActiveSheet.Cells(j, i).Formula = "= BDH(""" & ticker & """,""Last_Price"",""" & time & """,""" & time & """, ""days=a"", ""fill=p"")"
            Application.OnTime Now + TimeValue("00:00:03"), "pastespecial"
            Next
        Next
    end sub
    'start a new sub to terminate previous macro
    sub pastespecial()
    Dim lastrow As Integer
    lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    Dim lastcol As Integer
    lastcol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
    Set myrange = Range(ActiveSheet.Cells(lastrow - 6, 3), ActiveSheet.Cells(lastrow, lastcol))
    myrange.Value = myrange.Value
    
    end sub
    

    【讨论】:

      猜你喜欢
      • 2013-05-27
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多