1

I'm trying to build a macro that inserts a value into a specific cell of table by looking up a row value and looking up a column value and using that to determine the cell to paste to. I am quite new to VBA so I'm not really sure what I'm doing.

This is what I have so far:

Name=Sheets("Input").Range("C5")
Week=Sheets("Input").Range("C19")
copyCell=Sheets("Input").Range("C18")

pasteCell = Application.Index(Sheets("Resources").Range("B2:AZ50"),Application.Match(Week,Sheets("Resources").Range("A2:A50"),0),Application.Match(Name,Sheets("Resources").Range("B1:AZ1"),0))

copyCell.Copy
pasteCell.PasteSpecial PasteValues

I keep getting a runtime error object required but I'm not quite sure what I'm doing wrong...

Any help would be appreciated!

UPDATE

This is the table which the cell is being pasted too (this table is not the real table just an example so as to hide the names in the table but it looks exactly the same except that the real table starts on the first row).

enter image description here

And this is the input page:

enter image description here

Note: I have since put in more code surrounding the week variable:

If WorksheetFunction.CountA(Sheets("Input").Range("C19")) = 0 Then
Week = Sheets("Input").Range("C20").Value
Else
Week = Sheets("Input").Range("C19").Value
End If

Very much appreciate the help you guys are giving!

4

1 回答 1

0

without seeing more of your code, I assume you have declared copyCell and pasteCell as Type Variant (if it is not declared, it is type Variant).

This will raise the error you describe.

Resolve it by properly declaring your variables, and using the Set keyword when assigning object variables.

Additionally, failing to declare Name and Week will raise the 424 Object Required error, too. They should be String data type, and assigned using the range .Value method. This error will persist, without more revision to your code, if either of the match functions returns an error (match not found).

I think this will work:

Sub Test()
Dim Name As String
Dim Week As Long
Dim copyCell As Range
Dim pasteCell As Range

Name = Sheets("Input").Range("C5").Value
Week = CLng(DateValue(Sheets("Input").Range("C19").Value))
Set copyCell = Sheets("Input").Range("C18")

If Not IsError(Application.Match(Week, Sheets("Resources").Range("A2:A50"), 0)) And _
    Not IsError(Application.Match(Name, Sheets("Resources").Range("B1:AZ1"), 0)) Then

    Set pasteCell = Application.Index(Sheets("Resources").Range("B2:AZ50"), _
        Application.Match(Week, Sheets("Resources").Range("A2:A50"), 0), _
        Application.Match(Name, Sheets("Resources").Range("B1:AZ1"), 0))

    copyCell.Copy
    pasteCell.PasteSpecial PasteValues
Else:
    MsgBox Name & " and/or " & Week & " not found!", vbInformation
End If
End Sub

UPDATED

Excel doesn't always know what to do with date values. that was the case here. On the worksheet, a date value is stored as a long integer. In the macro, we're referring to it as a string, which can raise several errors including '1004' (unable to match).

I made a revision to the code above, Dim Week as Long and then changed the assignment to ensure the value is interpreted as a Date from the worksheet, and then as a Long integer when assigned to this variable:

Week = CLng(DateValue(Sheets("Input").Range("C19").Value))

Now, this may not be foolproof if your dates aren't all actually dates (google it...) but this actually happens pretty often some dates are entered as dates (numbers formatted as date) and others are entered as string literals. If that is the case, the Week value should still be handled properly, but the lookup range also should be formatted the same way to avoid potential errors again.

于 2013-07-29T23:26:22.527 回答