VBA - 获取值运行时错误1004

时间:2018-02-15 03:24:08

标签: vba runtime

这是我获取值函数的代码。我认为我的代码很好但是“GetValue = ExecuteExcel4Macro(arg)”行有一个错误。由于我想要从中提取值的已关闭工作簿中的单元格包含公式,并且程序无法识别计算值。我怎么能解决这个问题?

Sub metric(O As Integer, d As Integer, Name As String)

If Name = "naive_season" Then
R = 4
ElseIf Name = "naive" Then
R = 5
ElseIf Name = "average_season" Then
R = 6
ElseIf Name = "average" Then
R = 7
ElseIf Name = "triangular_average_season" Then
R = 8
ElseIf Name = "triangular_average" Then
R = 9
ElseIf Name = "exponential_season" Then
R = 10
ElseIf Name = "exponential" Then
R = 11
ElseIf Name = "multiplicative_season" Then
R = 12
ElseIf Name = "multiplicative_agg.season" Then
R = 13
End If

Dim Ori As String
Dim des As String
Ori = O
des = d

Dim p As String, f As String, s As String, a As String

p = "G:\1 month's results\Time series\" & Ori & "\daily_forecast\"
f = Ori & "_" & des & "_daily.xlsx"
s = "Sheet1"  'This is the name of the Sheet in your file
'a = "" 'Range of the value you want I'm not sure if you can pull more than cell's

Dim estimation As Worksheet
Set estimation = Workbooks("daily_forecast_results").Sheets(Ori)


'RMSE
a = "S7"
estimation.Cells(R, d + 2) = GetValue(p, f, s, a)

'(1) NRMSE
a = "S8"
estimation.Cells(R + 14, d + 2) = GetValue(p, f, s, a)

'(2) NRMSE
a = "S9"
estimation.Cells(R + 28, d + 2) = GetValue(p, f, s, a)

'MAE
a = "S12"
estimation.Cells(R + 42, d + 2) = GetValue(p, f, s, a)

'(1) NAME
a = "S13"
estimation.Cells(R + 56, d + 2) = GetValue(p, f, s, a)

'(2) NAME
a = "S14"
estimation.Cells(R + 70, d + 2) = GetValue(p, f, s, a)

'MAPE
a = "S10"
estimation.Cells(R + 84, d + 2) = GetValue(p, f, s, a)


End Sub

Function GetValue(Path, file, sheet, ref)
'   Retrieves a value from a closed workbook
    Dim arg As String
'   Make sure the file exists
    If Dir(Path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
'   Create the argument
    arg = "'" & Path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)
'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)

End Function

0 个答案:

没有答案