Run time Error 9 Help!!

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
 
Fluff,
From the very last name "Woods" (currently) to the first A last name there are no blanks. Below "Woods" there are blanks in column B but I think you meant in between the last last name and the first last name correct? There are no spaces.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Ok, which line gives the error?
 
Upvote 0
Glad you've sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,422
Messages
6,124,808
Members
449,191
Latest member
rscraig11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top