LikeButtah1

New Member
Joined
Apr 17, 2018
Messages
34
I am getting a run time error '9' subscript out of range (in red) and I'm unsure why since it was working and now not working. I obviously changed something but I'm unsure what I did. Any help with this code would be appreciated. Thanks.

Rich (BB code):
Sub DistributeFromBiWeeklyPayroll()

  Dim R As Long, LastRow As Long, NextRow As Long, ws As Worksheet
  Dim LastNames As Variant, QData As Variant
  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
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  LastNames = Range("B4", Cells(LastRow, "B"))
  QData = Range("O4", Cells(LastRow, "O"))
  For R = 1 To UBound(LastNames)
    Set ws = Sheets(LastNames(R, 1))
    NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "J").End(xlUp).Row) + 1
    ws.Cells(NextRow, "J") = QData(R, 1)
  Next
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  LastNames = Range("B4", Cells(LastRow, "B"))
  QData = Range("J4", Cells(LastRow, "J"))
  For R = 1 To UBound(LastNames)
    Set ws = Sheets(LastNames(R, 1))
    NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "F").End(xlUp).Row) + 1
    ws.Cells(NextRow, "F") = QData(R, 1)
  Next
   LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  LastNames = Range("B4", Cells(LastRow, "B"))
  QData = Range("K4", Cells(LastRow, "K"))
  For R = 1 To UBound(LastNames)
    Set ws = Sheets(LastNames(R, 1))
    NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "G").End(xlUp).Row) + 1
    ws.Cells(NextRow, "G") = QData(R, 1)
  Next
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  LastNames = Range("B4", Cells(LastRow, "B"))
  QData = Range("L4", Cells(LastRow, "L"))
  For R = 1 To UBound(LastNames)
    Set ws = Sheets(LastNames(R, 1))
    NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "H").End(xlUp).Row) + 1
    ws.Cells(NextRow, "H") = QData(R, 1)
  Next
      LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  LastNames = Range("B4", Cells(LastRow, "B"))
  QData = Range("M4", Cells(LastRow, "M"))
  For R = 1 To UBound(LastNames)
    Set ws = Sheets(LastNames(R, 1))
    NextRow = Application.Max(5, ws.Cells(ws.Rows.Count, "I").End(xlUp).Row) + 1
    ws.Cells(NextRow, "I") = QData(R, 1)
  Next
  Range("A1").Select
End Sub
 
Getting the values distributed but they are not all in the same row. f|c is good but all the other distributions are skipping a row.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Thank you for your help JackDanIce but I finally figured out the problem with the original code. In column B the list of Last Names had a break in the list of about 4 rows. Apparently the original code cannot skip these empty rows and continue to distribute the values hence the run time '9' error. I did try your code but the distribution of values started on one row then skipped a row and continued so it did not line up correctly as we would need the payroll to do. Anyway thanks again for your assistance it is greatly appreciated.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,820
Members
449,469
Latest member
Kingwi11y

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