为什么会出现错误“应用程序定义的错误或对象定义的错误”?

时间:2018-12-27 22:54:11

标签: excel vba

错误application-defined or object defined error来自哪里?它似乎来自我创建的公式。请帮忙。在VBA中编写代码确实不应该那么困难,但是由于某种原因它不起作用。

Sub get_levels()

    Dim Count As Integer
    Dim ticker As Variant
    Dim lastRow As Long
    Dim lastRowC As Long
    Dim rng As Range
    Dim current_input_position As Long
    Dim sheet As String
    Dim mula As String
    Dim updatedTicker As String

    Application.CutCopyMode = False
    Count = 0
    sheet = "Test_Sheet"

    current_input_position = 2

    lastRow = Cells(Rows.Count, "A").End(xlUp).row

    Label:

    Set rng = Range("A" & current_input_position & ":A" & lastRow)
    For Each ticker In rng.Cells

        lastRowC = Cells(Rows.Count, "C").End(xlUp).row
        updatedTicker = ticker & " A" & " Mtge"

        MsgBox updatedTicker 

        mula = "GCBDC 2018-1A A Mtge"

        Range("E2").formula = "=BDS(" & mula & ",""MTGE_CMO_GROUP_LIST"",""Headers=N"")"

    Next ticker


End Sub

1 个答案:

答案 0 :(得分:2)

假设您的Bloomberg加载项正常工作(尝试从常规工作表中手动调用BDS进行检查),我认为您可能需要更改此行:

Range("E2").formula = "=BDS(" & mula & ",""MTGE_CMO_GROUP_LIST"",""Headers=N"")"

Range("E2").Formula = "=BDS(""" & mula & """,""MTGE_CMO_GROUP_LIST"",""Headers=N"")"

如果变量mula中有空格(即使没有),则可能需要在两边都"

(此外,您的大多数RangeCells引用都未指定工作表或工作簿,这意味着它们将引用代码运行时活动的任何工作簿和工作表。讲,您不想要这个,但这不是您的问题。)