0

this should hopefully be an easy fix. So the code I have below runs fine when I only have 2 parameters from the first sheet being transferred to the second sheet, however when I try to add a third parameter (.Cells(c.row, "F"), my code receives the following error "compile error wrong number of arguments or invalid property assignment". Any thoughts as to why I can't seem to make my code work by adding more parameters when I need to be able to pull additional fields?

Sub copylist()
Dim wb As Workbook
Dim ws_employment As Worksheet
Dim c As Range
Dim i As Long

Set wb = ThisWorkbook
Set ws_employment = wb.Sheets("Employment")

'***This code fails due to adding ws_employment.cells(i, "F") & .cells(c.row, "F")***
'this will add values to the top table on the second sheet if there is an N value
i = 3 'this is the first row in the target sheet
With ws_employment_data
    For Each c In .Range("C3:C11").Cells
        If c.Value = "N" Then
            ws_employment.Range(ws_employment.Cells(i, "G"), ws_employment.Cells(i, "H"), ws_employment.Cells(i, "F")).Value = _
            .Range(.Cells(c.Row, "D"), .Cells(c.Row, "E"), .Cells(c.Row, "F")).Value
            i = i + 1
        End If
    Next c
'this will add values to the bottom table on the second sheet if there is a Y value
i = 12
    For Each c In .Range("C3:C11").Cells
        If c.Value = "Y" Then
            ws_employment.Range(ws_employment.Cells(i, "G"), ws_employment.Cells(i, "H")).Value = _
            .Range(.Cells(c.Row, "D"), .Cells(c.Row, "E")).Value
            i = i + 1
        End If
    Next c
End With



'***This code (is the same as above) works whilst having removed ws_employment.cells(i, "F") & .cells(c.row, "F")***
'this will add values to the top table on the second sheet if there is an N value
i = 3 'this is the first row in the target sheet
With ws_employment_data
    For Each c In .Range("C3:C11").Cells
        If c.Value = "N" Then
            ws_employment.Range(ws_employment.Cells(i, "G"), ws_employment.Cells(i, "H"), ws_employment.Cells(i, "F")).Value = _
            .Range(.Cells(c.Row, "D"), .Cells(c.Row, "E"), .Cells(c.Row, "F")).Value
            i = i + 1
        End If
    Next c
'this will add values to the bottom table on the second sheet if there is a Y value
i = 12
    For Each c In .Range("C3:C11").Cells
        If c.Value = "Y" Then
            ws_employment.Range(ws_employment.Cells(i, "G"), ws_employment.Cells(i, "H")).Value = _
            .Range(.Cells(c.Row, "D"), .Cells(c.Row, "E")).Value
            i = i + 1
        End If
    Next c
End With

MsgBox ("DONE")

End Sub
6
  • 1
    I think instead of ws_employment.Range(ws_employment.Cells(i, "G"), ws_employment.Cells(i, "H"), ws_employment.Cells(i, "F")).Value you want ws_employment.Range("F" & i & ":H" & i).Value? Commented Aug 15, 2021 at 19:40
  • .Range(.Cells(c.Row, "D"), .Cells(c.Row, "E"), .Cells(c.Row, "F")).Value needs to change too. D to F. Commented Aug 15, 2021 at 19:41
  • Hi @SiddharthRout, that didn't seem to work. Commented Aug 16, 2021 at 1:42
  • Hi @GokhanAycan, I'm confused by what you're trying to say, the values need to change to what exactly? Commented Aug 16, 2021 at 1:43
  • What do you mean by "that didn't seem to work" Commented Aug 16, 2021 at 5:18

1 Answer 1

2

The Range property only accepts 2 parameters (documentation) so you can only specify the 1st cell and the last cell.

I have also removed wb variable as it doesn't seems necessary.

Try this code:

Sub copylist()

    Dim ws_employment As Worksheet
    Dim c As Range
    Dim i As Long

    Set ws_employment = ThisWorkbook.Sheets("Employment")

    'this will add values to the top table on the second sheet if there is an N value
    i = 3 'this is the first row in the target sheet
    With ws_employment_data
        For Each c In .Range("C3:C11").Cells
            If c.Value = "N" Then
                ws_employment.Range(Replace("F?:H?", "?", i)).Value = _
                .Range(Replace("D?:F?", "?", c.Row)).Value
                i = i + 1
            End If
        Next c
    'this will add values to the bottom table on the second sheet if there is a Y value
    i = 12
        For Each c In .Range("C3:C11").Cells
            If c.Value = "Y" Then
                ws_employment.Range(ws_employment.Cells(i, "G"), ws_employment.Cells(i, "H")).Value = _
                .Range(.Cells(c.Row, "D"), .Cells(c.Row, "E")).Value
                i = i + 1
            End If
        Next c
    End With
    
    MsgBox "DONE"

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @Raymond Wu, can you explain what is supposed to take the place of the (?). I assume once I fill in that blank, then that will get rid of the "Object required error" that I receive. Thanks!
@bgado The ? Are supposed to be replace with the row number e.g. if i = 3 then it will become F3:H3. Which line did you get the error?
I went ahead and filled in the ?'s with 3 or whatever row number necessary but still receive the Object Required Error. It begins during the (For Each c in .Range("C3:C11").Cells row
Do you have a worksheet with the codename ws_employment_data? If not then you need to change it @bgado

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.