Home » excel » excel – "Method 'Range' of object '_Worksheet' failed" error message

excel – "Method 'Range' of object '_Worksheet' failed" error message

Posted by: admin April 23, 2020 Leave a comment


I have tried the code suggested in the question linked at the bottom but get an error on the line:

Set getNthColumn = Range(startCell, endCell)

The error is

Method ‘Range’ of object ‘_Worksheet’ failed

This code is supposed to set the colour formatting of a range cells whose formula is a VLOOKUP.

All help appreciated.

Vlookup to copy color of a cell – Excel VBA

How to&Answers:

Apparently you put the code into a worksheet’s code module.

In that case Range() resolves to Worksheet.Range() of that worksheet. If startCell or endCell belong to a different sheet you will get an error.

If you put the code in a plain module, as the OP specifically requested, Range() would then mean Application.Range(), and that would properly construct your range.

You can get rid of that ambiguity by explicitly using Application.Range(startCell, endCell).