2017-06-30 51 views
0

我對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循環,並突出顯示循環的第二行。提前謝謝你的幫助!非常感謝:)

回答

-1

嘗試設置的所有工作表中的變量

例如:

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

然後使用類似:

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

歡迎來到SO拉斐爾!只是一個提示,一定要解釋不僅是什麼,而且是爲什麼。這將有助於您的答案對於OP更清晰。祝你好運! –

+0

Thx諮詢:) –

+0

對工作表的緩存引用可能會稍微提高性能,但這不是問題的原因。 – GSerg

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?

+0

嗨布蘭登,謝謝你的幫助!關於如何編輯或更改代碼以使其可用,您有任何建議嗎?此外,什麼是不合格的細胞參考,我如何克服它們? – Bert

+0

上面的答案實際上是破壞你的代碼的問題,所以你需要從那裏開始修復它。一個非限定引用基本上是對父對象沒有指定的對象的引用。例如'Worksheets()'將是不合格的,而'ThisWorkbook.Worksheets'將完全合格。看看回答相同問題的其他帖子,那裏還有其他信息。我的建議是使用'With'塊和變量清理你的代碼,然後從那裏開始。 –

+0

非常感謝您的幫助,我會嘗試使用您的建議,看看它是怎麼回事:) – Bert