当前,我有一个宏,该宏使用控制文件在外部文件列表中循环执行以下过程:
由于要打开的文件数(30),并且所述文件的性质有点大(平均15k KB),因此我一直在尝试优化脚本。
当前,我正在使用.range方法替换剪贴板(复制/粘贴)的所有用法,其中源范围等于目标范围。这正在处理多个项目,但是我遇到了需要移至控制文件的特定范围的问题。
范围从源到目的地正确移动,但是一旦到达目的地,正在移动的范围中的第四列数据将导致#N / A。源数据全部由公式驱动(与硬编码数据相比),因此仅第4列出现此问题似乎很奇怪。第四列的唯一不同之处在于,它与前三列不连续,而是命名范围的第四列。
这是导致#N / A的特定代码段:
**Workbooks(Control_File).Sheets(Project).Range(ProjRange).Value =
Workbooks(Open_ES).Sheets("Sale Refi Dist. Template").Range(Project &
"_Tranche_1").Value**
下面是完整的脚本:
Sub NAV_Waterfalls()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
As_Of_Date = Range("As_Of_Date")
Control_File = Range("Control_File_Name")
Set Project_Rng = Range("Project_Range")
Dim cell As Range
Dim FPath As String
Z = Range("Project_Range").Rows.Count
For Each cell In Project_Rng
Workbooks(Control_File).Sheets("Asset Control - NAV").Activate
Project = cell
FPath = Range(Project & "_FilePath")
Open_ES = Project & " - Equity"
NAV = Range(Project & "_NAV")
Dim book As Workbook
Set book = Workbooks.Open(FPath, 2, ReadOnly:=True)
'Delete last run data
Workbooks(Control_File).Sheets(Project).Activate
Rows("7:500").Select
Selection.ClearContents
'Insert Date
Workbooks(Control_File).Activate
Workbooks(Control_File).Sheets("Asset Control -
NAV").Range("As_of_Date").Copy Workbooks(Open_ES).Sheets("Sale Refi Dist.
Template").Range(Project & "_Date_Input")
'Insert Project NAV
Workbooks(Control_File).Activate
Workbooks(Control_File).Sheets("Asset Control - NAV").Range(Project &
"_NAV").Copy Workbooks(Open_ES).Sheets("Sale Refi Dist.
Template").Range(Project & "_NAV_Input")
'Tranche 1
On Error Resume Next
With Workbooks(Open_ES).Sheets("Sale Refi Dist. Template").Range(Project &
"_Tranche_1")
Tranche_Rows = .Rows.Count
Tranche_Cols = 4
End With
Workbooks(Control_File).Sheets(Project).Activate
Range("A" & Cells.Rows.Count).End(xlUp).Select
ActiveCell.Offset(1).Select.Activate
StartRow = ActiveCell.Row
Lastrow = Tranche_Rows
Let ProjRange = "A" & StartRow & ":" & "D" & StartRow + Lastrow - 1
**Workbooks(Control_File).Sheets(Project).Range(ProjRange).Value =
Workbooks(Open_ES).Sheets("Sale Refi Dist. Template").Range(Project &
"_Tranche_1").Value**
Application.CutCopyMode = False
Workbooks(Open_ES).Close SaveChanges:=False
Workbooks(Control_File).Save
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
实际脚本的某些部分重复了“付款1”部分,以进行其他付款,但是对于所有付款,它可能都将重新编写为循环,因此我不介绍此部分。