我的宏需要使用“R.ajseasonX13”函数执行季节性调整(这是使用BERT工具包进行的,该工具包允许用户从R创建Execl函数,但这对答案来说并不重要,只是让你知道我想要完成的事情。)
我希望将函数应用于某个范围。为了让VBA更快地运行代码,我打算转换数组中的范围(代码中的“nsaArray”),将“R.ajseasonx13”函数(这是一个CSE公式)应用于数组的每一列并将所有这些变换列(它们在每次执行循环时由变量“sa”生成)放在一个新数组中,从而创建一个保持调整后的值的“临时矩阵”。有了这个矩阵,我想简单地在另一个工作表中打印它的值。
我可以定义数组,循环遍历并正确应用函数,但是当安装具有所需值的新矩阵时,我失败了,因为工作表范围会产生空白单元格。
如何在每个循环运行后在新数组中为“sa”变量(数组中每列的向量)分配所有值?
这是我的代码:
Sub Dessaz()
Dim wb1 As Workbook
Set wb1 = ActiveWorkbook
Dim wsNSA As Worksheet
Set wsNSA = wb1.Worksheets("NSA")
Dim wsSA As Worksheet
Set wsSA = wb1.Worksheets("SA")
Dim col As Range
Dim nsaArray() As Variant
'Finds the row of the first cell with numeric data in column A (where dates are stored)
wsNSA.Range(Cells(1, 1), Cells(1048576, 1)).NumberFormat = "General"
datas_col = wsNSA.Range(Cells(1, 1), Cells(10000, 1))
data1_linha = Application.Match(True, Application.Index(Application.IsNumber(datas_col), 0), 0)
wsNSA.Range(Cells(1, 1), Cells(1048576, 1)).NumberFormat = "dd/mm/yyyy"
'Determinates one of the parameters of the user defined function "R.ajseasonX13" used ahead
inicio = wsNSA.Cells(data1_linha, 1).Value
inicio = Year(inicio) & "-" & Month(inicio) & "-" & "01"
'LR is the last column with data and LC is the last column with data
LR = wsNSA.Cells(data1_linha, 1).End(xlDown).Row
LC = wsNSA.Cells(LR, 1).End(xlToRight).Column
'States another one of the parameters of the user defined function
p = 12
'Working with arrays instead of ranges in VBA
'ReDim nsaArray(1 To LR - 1 - num_linha, 1 To LC - 1)
nsaArray = wsNSA.Range(wsNSA.Cells(1, 2), wsNSA.Cells(LR, LC)).Value
MsgBox (LBound(nsaArray, 1))
For i = LBound(nsaArray, 2) To UBound(nsaArray, 2)
x = Application.Index(nsaArray, 0, i)
sa = Application.Run("R.ajseasonX13", x, inicio, p)
For j = LBound(nsaArray, 1) To UBound(nsaArray, 1)
nsaArray(j, i) = sa
Next
Next
wsSA.Range(wsSA.Cells(1, 2), wsSA.Cells(LR, LC)) = nsaArray
End sub