应用程序定义或对象定义错误 - 取决于打开哪个工作表

时间:2016-01-21 09:54:00

标签: vba excel-vba excel

我遇到了一个我正在努力解决的问题,因为它有点具体。我的代码可以从一个工作表复制并粘贴到其他工作表。代码的每个部分基本上将部分从主表“current”复制到指定的表单。

当我运行我的代码时,我收到错误“应用程序定义或对象定义的错误”,代码在工作表“Dividend yield”后停止在以下行之后

Worksheets("div. yield").Range("B7").Select

但是,如果我打开工作表“Dividend yield”并从那里运行我的代码,它将正常工作,直到最后一页“反向PE”,它将再次抛出并在行<}之后出现“应用程序定义或对象定义错误” / p>

Worksheets("Reverse_PE").Range("B9").Select

我猜错误与使用自动填充方法的下一行有关,但我没有找到任何有用的解决方案来解决这个问题。有人可以告诉我如何解决这个错误吗?

完整的宏代码如下。

Function getYield() As Double
    Dim appIE As Object
    Set appIE = CreateObject("internetexplorer.application")

    With appIE
    .Navigate "http://uk.investing.com/rates-bonds/world-government-bonds"
    .Visible = False
    End With

    Do While appIE.Busy
        DoEvents
    Loop

    Set allRowOfData = appIE.document.getElementById("pair_23705")
    Dim myValue As Double: myValue = allRowOfData.Cells(2).innerHTML
    appIE.Quit
    Set appIE = Nothing
    Worksheets("Reverse_PE").Range("B7").Value = myValue
    Worksheets("Reverse_PE").Range("B7").Value = Worksheets("Reverse_PE").Range("B7").Value / 100

End Function

Sub adjust()
    Dim copyAdress As Range
    Dim copyRange As Range
    Dim lastRow As Long
    Dim Median As Range


    '''PE'''
    Set copyAdress = Worksheets("current").Range("A1:CJ10000").Find("PE_RATIO", lookat:=xlPart)
    lastRow = Cells(65536, copyAdress.Column).End(xlUp).Row
    Set copyRange = Worksheets("current").Range(Cells(copyAdress.Row + 1, copyAdress.Column), Cells(lastRow, copyAdress.Column))
    Worksheets("PE").Range("B1").EntireColumn.Insert
    copyRange.Copy Destination:=Sheets("PE").Range("B7", "B" & lastRow)
    Worksheets("PE").Range("B2").Value = Worksheets("current").Range("A1").Value
    Worksheets("PE").Range("B3").FormulaArray = "=MEDIAN(B7:B" & lastRow + 2 & ")"
    Worksheets("PE").Range("B5").Font.Bold = True
    Worksheets("PE").Range("B5").FormulaArray = "=IF(ISNUMBER(VLOOKUP($A$5,$A$7:$HI$1750,COLUMN(B4),FALSE)),VLOOKUP($A$5,$A$7:$HI$1750,COLUMN(B4),FALSE)," & Chr(34) & NA & Chr(34) & ")"
    Set copyRange = Worksheets("current").Range("A5", "A" & lastRow)
    copyRange.Copy Destination:=Sheets("PE").Range("A7", "A" & lastRow + 2)


    ''Dividend yield'''
    Set copyRange = Worksheets("current").Range("A5", "A" & lastRow)
    copyRange.Copy Destination:=Sheets("div. yield").Range("A7", "A" & lastRow + 2)
    Worksheets("div. yield").Range("B7").FormulaArray = "=IF(ISNUMBER(current!X5),current!X5," & Chr(34) & Chr(34) & ")"
    Worksheets("div. yield").Range("B7").Select
    Selection.AutoFill Destination:=Sheets("div. yield").Range("B7:B" & lastRow + 2), Type:=xlFillDefault


    '''PE Forward'''
    Set copyAdress = Worksheets("current").Range("A1:CJ10000").Find("P/E-Ratio 03E", lookat:=xlPart)
    lastRow = Cells(65536, copyAdress.Column).End(xlUp).Row
    Set copyRange = Worksheets("current").Range(Cells(copyAdress.Row + 3, copyAdress.Column), Cells(lastRow, copyAdress.Column))
    Worksheets("PE_forward").Range("B1").EntireColumn.Insert
    copyRange.Copy Destination:=Sheets("PE_forward").Range("B7", "B" & lastRow + 2)
    Worksheets("PE_forward").Range("B2").Value = Worksheets("current").Range("A1").Value
    Worksheets("PE_forward").Range("B3").FormulaArray = "=MEDIAN(B7:B" & lastRow + 2 & ")"
    Worksheets("PE_forward").Range("B5").Font.Bold = True
    Worksheets("PE_forward").Range("B5").FormulaArray = "=IF(ISNUMBER(VLOOKUP($A$5,$A$7:$HI$1750,COLUMN(B751),FALSE)),VLOOKUP($A$5,$A$7:$HI$1750,COLUMN(B751),FALSE)," & Chr(34) & NA & Chr(34) & ")"
    Worksheets("PE_forward").Columns("B").Replace What:="#VALUE!", Replacement:=""
    Worksheets("PE_forward").Range("B3").NumberFormat = ""
    Set copyRange = Worksheets("current").Range("A5", "A" & lastRow)
    copyRange.Copy Destination:=Sheets("PE_forward").Range("A7", "A" & lastRow + 2)


    '''Reverse PE'''
    Set copyRange = Worksheets("current").Range("A5", "A" & lastRow)
    copyRange.Copy Destination:=Sheets("Reverse_PE").Range("A9", "A" & lastRow + 4)
    Worksheets("Reverse_PE").Range("B1").EntireColumn.Insert
    Worksheets("Reverse_PE").Range("B2").Value = Worksheets("current").Range("A1").Value
    Worksheets("Reverse_PE").Range("B5").FormulaArray = "=IF(ISNUMBER(VLOOKUP($A$5,$A$9:$HI$1750,COLUMN(B751),FALSE)),VLOOKUP($A$5,$A$9:$HI$1750,COLUMN(B751),FALSE)," & Chr(34) & NA & Chr(34) & ")"

    getYield
    Worksheets("Reverse_PE").Range("B3").FormulaArray = "=MEDIAN(B9:B" & lastRow + 4 & ")"

    Worksheets("Reverse_PE").Range("B9").FormulaArray = "=IF(ISNUMBER(PE!B7),1/PE!B7," & Chr(34) & Chr(34) & ")"
    Worksheets("Reverse_PE").Range("B9").Select
    Selection.AutoFill Destination:=Sheets("Reverse_PE").Range("B9:B" & lastRow + 4), Type:=xlFillDefault
    Worksheets("Reverse_PE").Range("B3:B" & lastRow + 4).Select
    Selection.Style = "Percent"
    Selection.NumberFormat = "0.00%"

1 个答案:

答案 0 :(得分:1)

除非工作表首次处于活动状态,否则无法使用select方法,因此请添加以下行:

Worksheets("div. yield").Activate
Worksheets("div. yield").Range("B7").FormulaArray = "=IF(ISNUMBER(current!X5),current!X5," & Chr(34) & Chr(34) & ")"

以后:

Worksheets("Reverse_PE").Activate
Worksheets("Reverse_PE").Range("B9").Select

有更快,更可维护的方式来做你想做的事情,但上面的工作表激活将解决你当前的问题。

在尝试选择其中一个单元格之前,请不要忘记激活每个工作表。