无法从某些工作表运行VBA宏

时间:2017-06-30 16:51:10

标签: vba

我对VBA很新,并且一直试图自动化一些财务报告。以下是我的子程序:

Sub normdata()

Dim numofstocks As Integer
Dim numofdatapoints As Integer
Dim numberofiterations As Integer
Dim averageposition As Integer

numofstocks = Application.CountA(Sheets("Static").Range("B:B")) - 1
Sheets("NormData").Range("A2").Value = "Date"

    For i = 1 To numofstocks
       Sheets("NormData").Cells(1, 2 * (i - 1) + 2).Value = Sheets("Static").Cells(i + 1, 1)
       Sheets("NormData").Cells(2, 2 * (i - 1) + 2).Value = "Close"
       Sheets("NormData").Cells(2, 2 * (i - 1) + 3).Value = "Returns"
    Next i

numofdatapoints = Application.CountA(Sheets("RawData").Range("A:A")) - 2

    For i = 1 To numofdatapoints
        Sheets("NormData").Cells(i + 2, 1).Value = Sheets("RawData").Cells(i + 2, 1).Value
    Next i

    For j = 1 To numofstocks
        For i = 1 To numofdatapoints
            Sheets("NormData").Cells(i + 2, 2 * (j - 1) + 2).Value = Sheets("RawData").Cells(i + 2, 6 * (j - 1) + 5).Value
        Next i
    Next j

numberofiterations = Application.CountA(Sheets("RawData").Range("A:A")) - 3
     For j = 1 To numofstocks
        For i = 1 To numberofiterations
            Sheets("NormData").Cells(i + 2, 2 * (j - 1) + 3).Value = (Sheets("NormData").Cells(i + 2, 2 * (j - 1) + 2).Value - Sheets("NormData").Cells(i + 3, 2 * (j - 1) + 2).Value) / Sheets("NormData").Cells(i + 3, 2 * (j - 1) + 2).Value
        Next i
    Next j


averageposition = Application.CountA(Sheets("NormData").Range("A:A")) + 2
    For i = 1 To numofstocks
        Worksheets("NormData").Cells(averageposition, 2 * (i - 1) + 2).Value = Worksheets("Static").Cells(i + 1, 1) & " average daily returns"
        Worksheets("NormData").Cells(averageposition, 2 * (i - 1) + 3).Value = Application.WorksheetFunction.Average(Worksheets("NormData").Range(Cells(3, 2 * (i - 1) + 3), Cells(numberofiterations + 2, 2 * (i - 1) + 3)))
        Worksheets("NormData").Cells(averageposition + 1, 2 * (i - 1) + 2).Value = Worksheets("Static").Cells(i + 1, 1) & " daily variance"
        Worksheets("NormData").Cells(averageposition + 1, 2 * (i - 1) + 3).Value = Application.WorksheetFunction.VarP(Worksheets("NormData").Range(Cells(3, 2 * (i - 1) + 3), Cells(numberofiterations + 2, 2 * (i - 1) + 3)))
        Worksheets("NormData").Cells(averageposition + 2, 2 * (i - 1) + 2).Value = Worksheets("Static").Cells(i + 1, 1) & " daily std dev"
        Worksheets("NormData").Cells(averageposition + 2, 2 * (i - 1) + 3).Value = (Application.WorksheetFunction.VarP(Worksheets("NormData").Range(Cells(3, 2 * (i - 1) + 3), Cells(numberofiterations + 2, 2 * (i - 1) + 3)))) ^ (1 / 2)
        Worksheets("NormData").Cells(averageposition + 3, 2 * (i - 1) + 2).Value = Worksheets("Static").Cells(i + 1, 1) & " 95% VaR"
        Worksheets("NormData").Cells(averageposition + 3, 2 * (i - 1) + 3).Value = Application.WorksheetFunction.Percentile(Range(Cells(3, 2 * i + 1), Cells(numberofiterations + 2, 2 * i + 1)), 0.05)
        Worksheets("NormData").Cells(averageposition + 4, 2 * (i - 1) + 2).Value = Worksheets("Static").Cells(i + 1, 1) & " 99% VaR"
        Worksheets("NormData").Cells(averageposition + 4, 2 * (i - 1) + 3).Value = Application.WorksheetFunction.Percentile(Range(Cells(3, 2 * i + 1), Cells(numberofiterations + 2, 2 * i + 1)), 0.01)
   Next i

    For i = 1 To numofstocks
        Worksheets("Static").Cells(1 + i, 4).Value = Worksheets("NormData").Cells(numberofiterations + 4, 2 * i + 1).Value
    Next i


End Sub

例如,我只能在表单“NormData”中运行代码,否则我得到运行时错误“1004”,应用程序定义或对象定义错误。代码总是在第二个for循环中停止,并突出显示循环的第二行。提前谢谢你的帮助!非常感谢:)

2 个答案:

答案 0 :(得分:1)

您的问题是您的代码使用了一堆非限定/隐式引用,当这些问题之一发生代码破坏时,它很难捕获。这一行就是问题所在:

Worksheets("NormData").Range(Cells(3, 2 * (i - 1) + 3), Cells(numberofiterations + 2, 2 * (i - 1) + 3)))

了解如何从ActiveWorkbook.Worksheets("NormData").Range开始,然后输入一个不合格的单元格引用Cells(3, 2 * (i - 1) + 3)?此单元格引用实际上显示为ActiveSheet.Cells(""),因此如果您的ActiveSheet不是ActiveWorkbook.Worksheets("NormData"),则代码将会中断。

查看此帖子了解详情:Why does Range work, but not Cells?

答案 1 :(得分:-1)

尝试在变量中设置所有工作表

例如:

dim wsNormData as Worksheet
set wsNormData = ThisWorkbook.Worksheets("NormData")

然后使用:

wsNormData.Cells(x,y).value = "value"