Can't find what's wrong with this VBA code - Runtime Error 1004

opazzo

Board Regular
Joined
Dec 21, 2005
Messages
65
Hi there,

I keep getting a Application defined or Object defined error on this piece of code, but can't see what's wrong. Have tried a few things but I feel i need a set a fresh eyes to look at it.

Here is the code :

Dim LastRow As Long
Dim WB As Workbook
Dim SPFWB As String
Dim SPFWS As String
Dim FirstBlank As String

SPFWB = ActiveWorkbook.Name
SPFWS = ActiveSheet.Name


LastRow = Workbooks(SPFWB).Worksheets(SPFWS).Cells(Rows.Count, 4).End(xlUp).Row
Set WB = Workbooks.Open(XXXX)

WB.Sheets("Register").Range("O:" & FirstBlank).FormulaR1C1 = _
"=INDEX( '[" & SPFWB & "]" & SPFWS & "'!R2C15:R" & LastRow & "C15,MATCH(RC[18], '[" & SPFWB & "]" & SPFWS & "'!R2C39:R" & LastRow & "C39,0))"

As you can see I'm basically trying to create an index/match formula with another workbook/worksheet. I believe the error in not in the formula itself but in somewhere in the syntax.
My guess is that it is the root cause is in the reference to the workbook/worksheet SPFWB / SPFWS .

Appreciate any help.

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You don’t say where the error is occurring but just an observation, where have you initialised “FirstBlank” variable?

Rich (BB code):
WB.Sheets("Register").Range("O:" & FirstBlank).FormulaR1C1

Dave
 
Upvote 0
You don’t say where the error is occurring but just an observation, where have you initialised “FirstBlank” variable?

Rich (BB code):
WB.Sheets("Register").Range("O:" & FirstBlank).FormulaR1C1

Dave


I've just copied some extracts of the code. Firstblank is basically the same as Lastrow.

The Vba error is on the formula.
 
Upvote 0
This line specifically :

Code:
WB.Sheets("Register").Range("O:" & FirstBlank).FormulaR1C1 = _
"=INDEX( '[" & SPFWB & "]" & SPFWS & "'!R2C15:R" & LastRow & "C15,MATCH(RC[18], '[" & SPFWB & "]" & SPFWS & "'!R2C39:R" & LastRow & "C39,0))"
 
Upvote 0
I'd suggest you assign the formula to a string variable and print that to the Immediate window. If it's correct, you should be able to copy and paste it into the cell. If not, correct the code until it is.
 
Upvote 0
Thanks guy

Found my issue as indicated in bold :


Rich (BB code):
WB.Sheets("Register").Range("O:" & FirstBlank).FormulaR1C1 = _
"=INDEX( '[" & SPFWB & "]" & SPFWS & "'!R2C15:R" & LastRow & "C15,MATCH(RC[18], '[" & SPFWB & "]" & SPFWS & "'!R2C39:R" & LastRow & "C39,0))"

Since I have a single cell in range, the column is an error.

Hope this can help others.
 
Upvote 0

Forum statistics

Threads
1,196,232
Messages
6,014,140
Members
441,805
Latest member
rwm

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