Method 'Intersect' of object'_Application' failed

dareman93

New Member
Joined
Jun 2, 2014
Messages
28
Hopefully someone out there can help me as I'm finding very few posts related to the actual error I'm receiving. Most are about Global failed instead of Application failed. I can find only a couple of posts actually related to the error I'm getting, which is Run-time error '1004': Method 'Intersect' of object '_Application' failed. And I apologize in advance for the length of this question. Hopefully all of the background is necessary based on what I'm thinking the error is a result of.

The error is being generated by a very large, complex Macro that is being used to update older versions of a spreadsheet we use for creating a capital improvement plan (for asset management) to the latest version. A separate copy of the workbook is used for each client and making all of the changes to the workbook for each client would be very time-consuming (and ripe with possibilities for errors). Thus the need to write the macro to do the updates consistently and error free.

Among other things that the macro does is to update the "Template" worksheet in the capital improvement plan (CIP) workbook. The "Template" worksheet is used within the CIP workbook to generate a new capital improvement project worksheet when new capital improvement projects are identified. It is generated when the user clicks on the "Create New Capital Improvement Project" button on the "General" worksheet of the CIP workbook. One cell of the existing "Template" worksheet is an overall budget number in current dollars. The update adds a new calculation below that for the estimated budgetary cost in the year the project will be done (i.e. taking up to 20 years' worth of inflation into account). This value is calculated on another worksheet named "Capital Expense Matrix". The calculation is a complex lookup formula in the form of an If statement. The overall if statement that gets put into each of the capital improvement project worksheets is as follows (actual cell references on the "Capital Expense Matrix" worksheet vary depending on the number of capital improvement projects included in the CIP workbook):
=IF(H3='Capital Expense Matrix'!C36, "Current Year", ROUND(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y30,2+MATCH(ROUNDUP(H3,0),'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))

To save you having to try to break that all down, that formula is an if statement that looks at the projected year of the capital improvement project in cell H3 and compares it to the year in the first column of the Capital Expense Matrix worksheet (the year the budgetary prices were developed). If the projected year of the project equals the year in the first column (i.e. if the project is supposed to be done in the year that the budgetary prices were developed), the formula returns a value of "Current Year" in the cell. See the attached images of the "Template" worksheet (with the formula cell highlighted) and a portion of the 'Capital Expense Matrix' worksheet for more details.

If the project is supposed to be done in any other year, the formula uses the VLOOKUP command to look up the name of the capital improvement project (in cell C7) in column B of the "Capital Expense Matrix" worksheet and returns the rounded, inflated cost (rounded up to the nearest $1,000) of the project in the year the project will be done using the MATCH function (the 3rd column of the VLOOKUP range is the current year, and the MATCH function returns the relative column number in the range of D1:Y1 that contains the year the project will be done - i.e. 2 + the column number returned by the MATCH command). The year in cell H3 is rounded to 0 because the workbook allows the user to put the capital improvement projects in order (if there are multiple projects in 1 year) by using a decimal point behind the year (i.e. the first project in 2020 could be 2020.01, the second could be 2020.02, the third could be 2020.03, etc.). Yes - very complicated, but it works like a charm...

...except on the "Template" worksheet. There is no "Template" project in the Capital Expense Matrix worksheet and both the year (in cell H3 of the "Template" worksheet) and the capital improvement project name (in cell C7 of the "Template" worksheet) are blank. As such, when entered into the cell on the "Template" worksheet, the formula results in a "#N/A" error. No big deal because, when the user creates a new capital improvement project using the "Create New..." button, that macro creates a copy of the "Template" worksheet and prompts the user for the year the project will be done and the project title. So, by the time the user sees the new project worksheet (the "Template" worksheet is hidden), the #N/A error is fixed because there is now a year and a title for the project.

However, that is where the "Update" macro is throwing the 'Intersect' of object error. The error is generated in the following lines of the macro:
With wsProj.Range("H5")
.Font.Bold = True
.HorizontalAlignment = xlCenter
.NumberFormat = "$#,###"
.Formula = "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
& "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
& ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
End With

The .Formula line is the one that is highlighted when you enter the debug mode. But, unlike normal debug mode, I can't drag the yellow arrow to a different part of the code. And the macro has apparently gone so far as inserting the formula because, as the attached Template CIP image shows, it's in the worksheet. But it then gets stuck on that .Formula line (after inserting the formula) and generates the error.

So, can anyone give me any guidance as to why that particular error is being generated or give me any ideas on how to get that formula into the Template worksheet? The formula needs to be there so that new projects created from the template have that inflated cost. I've come up with a couple of work arounds:
1) Edit the "Create New..." macro to have it insert the formula on the new capital improvement project worksheet after the year and project title have been entered.
2) Put a hidden (white text) "Template" project in the bottom row of the Capital Expense Matrix worksheet so the formula does not generate the #N/A error.

I haven't tested either of those work-arounds yet (number 1 would be a bugger because then I'd have to replace one of the workbook macros and I'd prefer not to go that route and number 2 might cause unforeseen errors with other parts of the workbook). But, even if one of those works, I would really like to figure out why this .Formula command is causing so much trouble. Thanks!!!
 

Attachments

  • Template CIP.jpg
    Template CIP.jpg
    115.9 KB · Views: 11
  • Capital Expense Matrix.jpg
    Capital Expense Matrix.jpg
    206.4 KB · Views: 10

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
In addition to the message from @davesexcel , above; try inserting the instruction "ClearContents /Debug.Print" in bertween your .NumberFormat and .Formula instructions:
Rich (BB code):
.NumberFormat = "$#,###"

.ClearContents
Debug.Print "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
& "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
& ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"

.Formula = "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
& "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
& ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
(blue lines)

Then, when the macro get the error and you enter the debug mode, check if the cell has a formula or is empty. If it's empty, open the vba "Immediate window" (Contr-g should do the job); copy the formula that has been "printed" in the window and try to set it into the targetted cell. Maybe the formula is formally incorrect, in that case you and us can focus on which formula you need to insert and why it is incorrect at that point.

Bye
 
Upvote 0
What would the formula look like on the sheet?
On the sheet, the formula looks like what I typed in on the original message (that probably got lost in the convoluted explanation of what I'm trying to do). The formula on the sheet (with the cells for this particular workbook, given the number of capital improvement projects on it) is:

=IF(H3='Capital Expense Matrix'!C36, "Current Year", ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y30,2+MATCH(ROUND(H3,0),'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))
 
Upvote 0
In addition to the message from @davesexcel , above; try inserting the instruction "ClearContents /Debug.Print" in bertween your .NumberFormat and .Formula instructions:
Rich (BB code):
.NumberFormat = "$#,###"

.ClearContents
Debug.Print "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
& "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
& ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"

.Formula = "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
& "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
& ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
(blue lines)

Then, when the macro get the error and you enter the debug mode, check if the cell has a formula or is empty. If it's empty, open the vba "Immediate window" (Contr-g should do the job); copy the formula that has been "printed" in the window and try to set it into the targetted cell. Maybe the formula is formally incorrect, in that case you and us can focus on which formula you need to insert and why it is incorrect at that point.

Bye

Interesting - I tried your suggestion, Anthony, and I'm now getting that same error kicking up on the .ClearContents line of the code. Same thing as when it kicked up on the formula - you can enter debug mode and edit the macro, look at locals, etc., but you can't move the yellow arrow indicating the current line of the macro being evaluated. You can drag the arrow up or down, but it won't let you select another line (kind of like everything is being viewed as a comment or something).

I also attempted to comment out the .NumberFormat = "$#,###" line of the code and it still kicks the error up highlighting the .ClearContents line.

In addition, I removed the With statement as the source of the error by changing that section of the macro to:
VBA Code:
        wsProj.Range("H5").Font.Bold = True
        wsProj.Range("H5").HorizontalAlignment = xlCenter
        wsProj.Range("H5").NumberFormat = "$#,###"
        wsProj.Range("H5").ClearContents
'            Debug.Print "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
                & "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
                & ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
        wsProj.Range("H5").Formula = "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
                & "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
                & ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
'        End With
The error is still generated on the wsProj.Range("H5").ClearContents line.

For giggles, I next took the code as written above, commented out the .ClearContents line and uncommented the Debug.Print line and re-ran the macro. It goes back to generating the error on the .Formula line of the code. The formula generated by the Debug.Print line is correct and works when pasted into the "Template" worksheet (it still results in the #N/A error, but that is fixed when you enter a year into cell H3 and a valid project name (one that appears in the "Capital Improvement Matrix worksheet") into cell C7.

Finally, I commented out that entire segment of the code and re-ran it. The same error was kicked out on the next segment of code right below the one that's been trouble all day:
Code:
'        With wsProj.Range("H5")
'            .Font.Bold = True
'            .HorizontalAlignment = xlCenter
'            .NumberFormat = "$#,###"
'            .ClearContents
'            Debug.Print "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
                & "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
                & ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
'            .Formula = "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
                & "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
                & ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
'        End With
        With wsProj.Range("I4")
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
            .NumberFormat = "General"
            .Formula = "=CONCATENATE(""("", 'Capital Expense Matrix'!C" & MatrixRow + 6 & "," _
                & " "" cost)"")"
            .FormatConditions.Delete
        End With

It now generates the error on the second .Formula line (.Formula = "=CONCATENATE(""("",....cost)"")". I think I need a beer - or 10!
 
Upvote 0
I should join you for the beer(s)...

No idea about what is going on there.
Try this stripped macro:
VBA Code:
Sub StrippedMacro()

Set wsProj = ThisWorkbook.Sheets("Foglio1")     '<<< YOUR setting
MatrixRow = 30
With wsProj.Range("H5")
    .NumberFormat = "$#,###"
    .ClearContents
    Debug.Print "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
    & "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
    & ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
    .Formula = "=IF(H3='Capital Expense Matrix'!C" & MatrixRow + 6 & ",""Current " _
    & "Year"",ROUNDUP(VLOOKUP(C7,'Capital Expense Matrix'!B2:Y" & MatrixRow _
    & ",2+MATCH(ROUND(H3,0)," & "'Capital Expense Matrix'!D1:Y1,0),FALSE),-3))"
End With
End Sub
Put it in a "Standard Module" of your Vba Project, customize the line marked <<< and run the macro
I'm trying to understand wether the problem is in the worksheet (probably not) or in the code.
Probably this stripped macro will set w/out any problem the formula in the target cell, and in that case we should try to understand what changes when your complete macro runs, and don't think it will be a walk.

Is this happening on more than 1 single PC? Do you know what triggered the start of the problem? Which Office version do you run? Can you execute the Office Repair procedure? Does this involve an open userform? Is wsProj an object of the active workbook (active when the failing code is executed)? Is your code inserted in a standard vba module, or a Class module (a userform, a Sheet Module, ThisWorkbook module)?
Anything that could lit the light is useful

Bye
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,163
Members
448,554
Latest member
Gleisner2

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