LikeButtah1
New Member
- Joined
- Apr 17, 2018
- Messages
- 34
I'm trying to distribute cells from a payroll sheet to individual employee sheets. It should find the last name on the payroll sheet in column B and distribute the pay or PTO used into the corresponding individual employee sheet but I keep getting Run Time Error 9 Subscript out of Range at the first Set ws = Sheets(LastNames(R, 1)) . The code is activated with a command button on the actual payroll sheet. I cannot see what is causing the error. Can someone take a look and let me know what I'm doing wrong. Thanks in advance.
VBA Code:
Sub DistributeFromBiWeeklyPayroll()
Dim R As Long
Dim LastRow As Long
Dim NextRow As Long
Dim ws As Worksheet
Dim LastNames As Variant
Dim QData As Variant
Application.ScreenUpdating = False
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
LastNames = Range("B4", Cells(LastRow, "B"))
QData = Range("F4", Cells(LastRow, "F"))
For R = 1 To UBound(LastNames)
Set ws = Sheets(LastNames(R, 1))
NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "C").End(xlUp).Row) + 1
ws.Cells(NextRow, "C") = QData(R, 1)
Next
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
LastNames = Range("B4", Cells(LastRow, "B"))
QData = Range("N4", Cells(LastRow, "N"))
For R = 1 To UBound(LastNames)
Set ws = Sheets(LastNames(R, 1))
NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "E").End(xlUp).Row) + 1
ws.Cells(NextRow, "E") = QData(R, 1)
Next