我对此很新(昨天开始),所以也许有人有一些建议。我创建了一个宏,它使用每月值的新列和公式更新下载表,同时保持每年和静态值不变。该代码执行其预期功能,而是通过穿过环需要近30分钟,以循环时库存= 25缩短VBA宏的运行时间
Sub New_monthly()
Dim i As Integer
Dim Measure As Integer
Dim Count As Integer
Dim Stock As Integer
Dim Monthend As Long
Stock = Application.WorksheetFunction.CountIf(Range("AP1:AP50"), ">0")
Monthend = Range("AS1").Value
For i = 0 To Stock - 1
' Copy old values and move them over one space
Range("B54:B115").Offset(i * 115, 0).Select
Selection.Insert Shift:=xlToRight
' Enter new values
Cells(108 + i * 115, 2).Value = Monthend
Cells(109 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(109 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Cells(110 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(110 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Cells(111 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(111 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Cells(112 + i * 115, 2).Formula = "=BDH(" & Cells(112 + i * 115, 1).Address & "," & Cells(111 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Cells(113 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(113 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Cells(114 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(114 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Cells(115 + i * 115, 2).Formula = "=BDH(" & Cells(100 + i * 115, 1).Address & "," & Cells(115 + i * 115, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
For Measure = 0 To 5
Cells(54 + i * 115 + Measure * 9, 3).Value = Monthend
For f = 0 To 7
Cells(55 + i * 115 + Measure * 9 + f, 3).Formula = "=BDH(" & Cells(19 + i * 115 + f, 1).Address & "," & Cells(54 + i * 115 + Measure * 9, 1).Address & ",$AS$1,$AS$1,""DAYS=C"")"
Next f
Cells(57 + i * 115 + Measure * 9, 3).Formula = "=AVERAGE(" & Cells(58 + i * 115 + Measure * 9, 3).Address & ":" & Cells(62 + i * 115 + Measure * 9, 3).Address & ")"
Next Measure
Next i
End Sub
既然你开始了,这里有一些提示...... 1.“CountIf”是找到最后一行的不可靠的方法。你可能想看到[This](http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba/11169920#11169920)2.避免使用'.Select '。你可能想看到[This](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)。如果他们是连续的,你可以在一个范围内一次输入公式。你不需要单独做。 –
对于Bloomberg来说,最好的方法是通过传递数组来调用Vba中的函数,将返回值存储在另一个数组中,并将结果数组转储到报表/数据表中。避免在单元格中写很多公式。特别是BDH是异步的,它的管理将变得非常混乱。 – cyboashu
谢谢,我将在未来记住这一点! –