runtime error 1004 - application defined or object-defined error

boukaka

New Member
Joined
Feb 26, 2014
Messages
2
I have a small macro that is manipulating columns and entering formula's in a couple of cells but for some reason when I try to set the vlookup formula I get a runtime error. (at this line ActiveCell.FormulaR1C1 = Formula)

If I paste the string that is in Formula into the cell manually the formula is fine and works perfectly. Can someone please tell me why I can't paste it in using vba code?


Sub Macro1()

Sheets("Sheet2").Select
Dim Check As String
Check = "D1:D" + CStr(LastRow(Sheets("Sheet2")))
Columns("A:A").Select
Selection.Cut
Columns("F:F").Select
Selection.Insert Shift:=xlToRight
Columns("A:B").Select
Selection.NumberFormat = "@"
Range("D1").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-3],RC[-2])"
Selection.AutoFill Destination:=Range(Check)
Sheets("Sheet1").Select
Range("A1").Select
ActiveCell.EntireColumn.Insert
Dim DestRange As String
DestRange = "A3:" + CStr(LastRow(Sheets("Sheet1")))
Dim Formula As String
Dim Rng As String
Rng = "$D$1:$E$" + CStr(LastRow(Sheets("Sheet2")))
Formula = "=VLOOKUP(CONCATENATE(C2,D2),Sheet2!" + Rng + ",2,FALSE)"
Range("A2").Select
ActiveCell.FormulaR1C1 = Formula
Selection.AutoFill Destination:=Range(DestRange)
Range("A1").Select
ActiveCell.Value = "NEW_UCID"
Range("B1").Select
ActiveCell.Value = "OLD_UCID"

End Sub
Function LastRow(ws As Object) As Long


Dim rLastCell As Object
On Error GoTo ErrHan
Set rLastCell = ws.Cells.Find("*", ws.Cells(1, 1), , , xlByRows, _
xlPrevious)
LastRow = rLastCell.Row


ErrExit:
Exit Function


ErrHan:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbExclamation, "LastRow()"
Resume ErrExit


End Function
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
try using & instead of + when building strings
 
Upvote 0
I don't thing the variable "formula" contains what you think it should. Try printing it in the debugger.
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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