Method 'Range' of object Worksheet failed

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi all,

My macro does this:

  1. Opens a new workbook from a specified file name
  2. Sets Sheets(1) worksheet as variable WS3
  3. Looks for the word "Date" in WS3
  4. Sets a variable "WorkingRange" as the cell it found, offset by 1 row and 0 columns
  5. Checks whether the month of "WorkingRange" matches the value in a named cell in another worksheet ("WS1")
Here is my code:


Excel Formula:
Set WorkingRange = WS3.UsedRange.Find("Date").Offset(1, 0)
    
    If WorkingRange <> "" Then
    
        If Month(WS3.Range("WorkingRange")) <> WS1.Range("RepMonth") Then
        
            CellA.Offset(0, 6) = "No"
            SubTest = False
            
        Else
        
            CellA.Offset(0, 6) = "Yes"
        
        End If
        
    Else
    
        CellA.Offset(0, 6) = "n/a"
    
    End If

At the first IF statement, I am getting a "Range of Object Worksheet failed" error.

When I hover over WorkingRange, I get the preview of "01/10/2022" - so it has found and offset properly.

So - which referencing protocol have I got wrong?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,

VBA Code:
    If Not WorkingRange Is Nothing Then

Holger
 
Upvote 0
Hi,

VBA Code:
    If Not WorkingRange Is Nothing Then

Holger

Hi - thanks! Sorry but I got my description wrong.

It is the SECOND if statement that is failing.

I have used your method elsewhere to test if it can be found, but here it is assuming the word is found and working from there.

So it is this line that is giving me the error:

VBA Code:
If Month(WS3.Range("WorkingRange")) <> WS1.Range("RepMonth") Then

I need it to test for "" because it might find Date in the header but there is no data - so that is why I worked it this way.

Thanks!
 
Upvote 0
Hi TheRedCardinal,

if WS1.Range("RepMonth") represents a range of one cell holding a number:

VBA Code:
Set WorkingRange = WS3.UsedRange.Find(what:="Date").Offset(1, 0)
If Not WorkingRange Is Nothing Then
  If Month(WorkingRange) <> RepMonth Then

Holger
 
Upvote 0
Solution
Hi TheRedCardinal,

if WS1.Range("RepMonth") represents a range of one cell holding a number:

VBA Code:
Set WorkingRange = WS3.UsedRange.Find(what:="Date").Offset(1, 0)
If Not WorkingRange Is Nothing Then
  If Month(WorkingRange) <> RepMonth Then

Holger
That's the one!
Thanks
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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