0

I'm not sure why I'm getting the Application-Defined or Object-Defined Error on the below line:

AllRng.Resize(hard - 1, 1).Value = p.Value

Which is under the "SkipHard:" section towards the bottom of the code.

I went through a few other similar posts on here and tried applying some recommendations, including changing Sheets to Worksheets, but I'm not able to figure it out.

When I insert the this error code, the macro seems to be working fine, but not sure if this is bad practice:

On Error Resume Next
//Line here causes 1004 error.
If Err.Number > 0 Then
    Debug.Print Err.Number & ":" & Err.Description
End If

*Additional Note: I've tried a number of different things now and also recreated the file I'm using multiple times. (It contains multiple macros). It seems like this problem only arises when I add this UDF into the workbook. Is that possible or am I off? Macro that looks up each value from one column and returns email address with ";" separator

Below is the entire code:

Option Explicit
Sub AssignBlocks()
    Dim Records As Long, PasteTo As Long, People As Integer, LastP As Long
    Dim balance As Integer, base As Integer, hard As Integer, p As Range
    Dim AllRng As Range, BaseStart As Integer

    'Copy/paste formats:
    Dim i As Long
    Dim var As Variant
    Dim FirstTab As Worksheet
    Dim SecondTab As Worksheet
    Set FirstTab = Application.Worksheets("Employees List")
    Set SecondTab = Application.Worksheets("Main")

    Application.ScreenUpdating = False

    'Assigning people to block:
    People = Sheets("Block Assignment").Cells(Rows.Count, 16).End(xlUp).Row
    If People <> 1 Then
        Sheets("Block Assignment").Range("P2:P" & People).Clear
    End If

    'Pulling people from this column:
    People = Sheets("Main").Cells(Rows.Count, 13).End(xlUp).Row
    On Error Resume Next
    Sheets("Block Assignment").Range("M2:M" & _
        People).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    On Error GoTo 0
    Records = Sheets("Block Assignment").Cells(Rows.Count, 1).End(xlUp).Row - 1
    People = Sheets("Main").Cells(Rows.Count, 13).End(xlUp).Row - 1
    base = Application.WorksheetFunction.RoundDown(Records / People, 0)
    balance = Records - (People * base)
    hard = base + 1
    Sheets("Main").Range("M2:M" & balance + 1).Name = "hardrng"
    Sheets("Main").Range("M" & balance + 2 & ":M" & People + 1).Name = "easyrng"
    If balance = 0 Then GoTo SkipHard
    For Each p In Sheets("Main").Range("hardrng")
        PasteTo = Sheets("Block Assignment").Cells(Rows.Count, 16).End(xlUp).Row + 1
        Set AllRng = Sheets("Block Assignment").Range("P" & PasteTo)
        AllRng.Resize(hard, 1).Value = p.Value
    Next p

    Skip hard:
    For Each p In Sheets("Main").Range("easyrng")
        PasteTo = Sheets("Block Assignment").Cells(Rows.Count, 16).End(xlUp).Row + 1
        Set AllRng = Sheets("Block Assignment").Range("P" & PasteTo)
        AllRng.Resize(hard - 1, 1).Value = p.Value
    Next p

    'Copy formats to column M's start:
    For i = 2 To 1000
        'How to only go to the last row with data?
        var = Application.Match(SecondTab.Range("M" & i), FirstTab.Range("A:A"), 0)
        If Not IsError(var) Then
            FirstTab.Range("A" & var).Copy
            SecondTab.Range("M" & i).PasteSpecial xlPasteFormats
            Application.CutCopyMode = False           
        End If
    Next i
    'Copy formats to column T's end:
    People = Sheets("Main").Cells(Rows.Count, 13).End(xlUp).Row
    LastP = Sheets("Block Assignment").Cells(Rows.Count, 16).End(xlUp).Row
    For Each p In Sheets("Main").Range("M2:M" & People)
        ActiveWorkbook.Sheets("Block Assignment").Range("P:P").AutoFilter _ 
            Field:=1, Criteria1:="" & p
        p.Copy
        On Error Resume Next
        ActiveWorkbook.Sheets("Block Assignment").Range("P2:P" & _ 
            LastP).SpecialCells(xlCellTypeVisible).PasteSpecial xlFormats
        On Error GoTo 0
        Application.CutCopyMode = False
    Next p
    ActiveWorkbook.Sheets("Block Assignment").AutoFilterMode = False
    Application.ScreenUpdating = True
    ThisWorkbook.Worksheets("Main").Range("A1").Select
End Sub
maestro
  • 17
  • 7
  • What's the value of `hard` when it fails? – Tim Williams Feb 27 '21 at 04:55
  • I'm not sure how to answer that? – maestro Feb 27 '21 at 05:06
  • i think it may be because it's referencing a column that has IFERROR formulas, which produced null if there is no result, and i'm getting the 1004 error because of those null values? I edited my post with some additional code that i placed before and after the line causing the 1004 error. – maestro Feb 27 '21 at 05:31
  • When it errors, click Debug and hover the mouse cursor over the `hard` variable - you should see a pop-up with the variable's value. `On Error Resume Next` is OK if you expect a line might error, but only if you know *why* it might error, and it you cancel it ASAP with `On Error Goto 0` - (otherwise you miss errors you should know about). – Tim Williams Feb 27 '21 at 06:04
  • thanks for the clarification Tim. The value for `hard` = 1. Yeah, I don't like the idea of using `On Error Resume Next` here, because I have no idea what's going on. – maestro Feb 27 '21 at 06:23
  • `AllRng.Resize(0, 1).Value = p.Value` will produce the error you mention. `hard - 1` will produce the 0 for the *Resize* function when `hard` = 1. – Variatus Feb 27 '21 at 06:52

1 Answers1

0

AllRng will always be valid so the error is that hard=1.

For hard to = 1, base must equal zero which means your rounddown is zero.

So you have a situation where Records (last value in column A) < People (Last value in column K)

So you need to control for this situation either in your code or at input - this is soething you will have to figure out based on the business context.

JohnnieL
  • 1,192
  • 1
  • 9
  • 15
  • Ahh thanks so much for the explanation Johnnie. I'm going to have to go through the code again. I must have accidentally changed something – maestro Feb 27 '21 at 08:29
  • I've tried a number of different things now and also recreated the file I'm using multiple times. (It contains multiple macros). It seems like this problem only arises when I add this UDF into the workbook. Is that possible or am I off? https://stackoverflow.com/questions/66253850/macro-that-looks-up-each-value-from-one-column-and-returns-email-address-with – maestro Feb 27 '21 at 17:56