我有一个要填充到模板中的数组,目标单元格是A3(前两行是标题)。数组填充了第一行员工,但是在这一行填充了数组的第一行之后,我得到了subscript out of range error
:
Dest.Offset(destcol, destrow) = Data(sourcerow, sourcecol)
其他脚本供参考:
Option Explicit
Sub Main()
Dim Wb As Workbook
Dim Data, Last, Login
Dim sourcerow As Long, sourcecol As Long, destrow As Long, destcol As Long
Dim Dest As Range
'Refer to the template
Set Wb = Workbooks("Default_Changes_Template.xlsx")
'Refer to the destination cell
Set Dest = Wb.Sheets("Sheet1").Range("A3")
'Read in all data
With ThisWorkbook.Sheets("Full Population")
Data = .Range("AL3", .Range("A" & Rows.Count).End(xlUp))
End With
Wb.Activate
Application.ScreenUpdating = False
'Process the data
For sourcerow = 1 To UBound(Data)
'Manager changes?
If Data(sourcerow, 1) <> Last Then
'Skip the first
If sourcerow > 1 Then
'Scroll into the view
Dest.Select
'Save a copy
Wb.SaveCopyAs ThisWorkbook.Path & Application.PathSeparator & _
ValidFileName(Login & " - " & Last & " - " & "Default Adjustments.xlsx")
End If
'Clear the employees
Dest.Resize(3, Columns.Count - Dest.Column).ClearContents
'Remember this manager
Login = Data(sourcerow, 2)
Last = Data(sourcerow, 1)
'Start the next round
destcol = 0
End If
'Write the employee data into the template
destrow = 0
For sourcecol = 1 To UBound(Data, 1)
Dest.Offset(destcol, destrow) = Data(sourcerow, sourcecol)
destrow = destrow + 1
Next
'Next column
destcol = destcol + 1
Next
End Sub