Search code examples
vbaexcelif-statementinvalid-argument

"Wrong number of arguments or invalid property assignment" Error when trying to delete values in cells


the segment of my code is as follows,

If Not Column2 = Empty Then
    Set Rg2 = sheet.Cells.Find(What:=Column2)
    If Not Rg2 Is Nothing Then
        Column2Width = Rg2.MergeArea.Columns.Count - 1
        StartCol2Column = Rg2.Column
        EndCol2Column = StartCol2Column + Column2Width
    Else
        MsgBox Column2 & " not found in " & BalkanSheet.Name
    End If
    Else
    Worksheets("Graph").Range(Cells(i, 7), Cells(i, 8), Cells(i, 9), Cells(i, 10)).Value = ""
End If

When i try to run it i get an error with the line Worksheets("Graph").Range(Cells(i, 7), Cells(i, 8), Cells(i, 9), Cells(i, 10)).Value = "", saying "Wrong number of arguments or invalid property assignment".

The i value is a variable that is set earlier in the code. What i want the code to do is if the If statement is false (Else) then it will delete the values in the cells specified.

Any ideas?


Solution

  • Try something like this:

    with Worksheets("Graph")
        .Range(.Cells(i, 7), .Cells(i, 10)).ClearContents
    end with
    

    The idea is that the . is a way to refer the worksheet and that is needed, when you refer to cells.