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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
That sounds as though one of the sheet names is wrong.
 
Upvote 0
Firstly qualify your ranges with a sheet name. That macro will have different action depending on active sheet when run. Then either a cell is blank or has a sheet that doesnt exist most likely.
 
Upvote 0
Is there a way to make this code skip a name that it cannot find? and go onto the next name? Thanks again in advance.
 
Upvote 0
How about
VBA Code:
  For R = 1 To UBound(LastNames)
    If Evaluate("isref('" & LastNames & "'!A1)") Then
      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)
   End If
  Next
 
Upvote 0
Why not remove it from your sheet? Im all for preventing errors but some errors you want to see. This looks like one of them.
 
Upvote 0
I agree Steve but this workbook will have name changes throughout the year with people being added (not a problem) but people also being removed since they quit or are terminated. What this creates is a constant updating of the workbook that I am trying to avoid.

Fluff I'm getting error 13 from your code but thanks.
 
Upvote 0
Oops, it should be
VBA Code:
  For R = 1 To UBound(LastNames)
    If Evaluate("isref('" & LastNames(R, 1) & "'!A1)") Then
      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)
   End If
  Next
 
Upvote 0
I think the easiest route is simply to make an employee sheet and then maybe hide the sheet? The code will run no problem then.
I was hoping for the code the skip the fact that it cannot find the employee sheet name and move on the the next last name in column B but this way works too.

Fluff- still get the error 13 but I really appreciate your help. Thanks.
 
Upvote 0
Do you have any blank cells in col B of the active sheet
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,834
Members
449,192
Latest member
mcgeeaudrey

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