Getting Range of Object - Worksheet Error in fully referenced code

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
In my code, I set a variable WS1 as follows:

VBA Code:
Set WBk1 = ThisWorkbook
Set WS1 = WBk1.Sheets("2. Final Data")

I then import a sheet from another workbook as follows:

VBA Code:
s = "<Full Path>"
t = Dir(s)

    If t = "" Then

        MsgBox ("The VN Product File could not be found - please select it to open")
        With Application.Dialogs(xlDialogOpen)
            If .Show = True Then
                Set WBk2 = ActiveWorkbook
            End If
        End With

        Else
    
            Workbooks.Open Filename:=s
            Set WBk2 = ActiveWorkbook
    
    End If
        
WBk2.Sheets(1).Copy Before:=WBk1.Sheets("Data Sheet")
WBk2.Close SaveChanges:=False
ActiveSheet.Name = "RP"

Then finally I attempt to load a column from WS1 into an array:

VBA Code:
Set CellA = WS1.Range("A1:Z1").Find("Item number")

WS1.Columns(CellA.Column + 1).Insert Shift:=xlToLeft
WS1.Cells(1, CellA.Column + 1) = "Origin"

'Collate Item Numbers into a Single Table

Set CellA = WS1.Range("A1:Z1").Find("Item")
LCol = WS1.Range("Final_Table[#Data]").Rows.Count

TableItems = WS1.Range(Cells(2, CellA.Column), Cells(LCol, CellA.Column))

TableItems has been declared as a Variant

But I get the Range of Object error in the final line where I allocate the column to Table Items.

I have hovered over the variables:

CellA.Column = 9
LCol = 80
WS1 = Worksheet/Sheet 3 - which is correct

What silly error have I made with this?

Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You didn't qualify the Cells properties with the sheet:

Code:
TableItems = WS1.Range(WS1.Cells(2, CellA.Column), WS1.Cells(LCol, CellA.Column))
 
Upvote 0
Solution
Glad to help.

By the way, instead of this:

Code:
            Workbooks.Open Filename:=s
            Set WBk2 = ActiveWorkbook

you should really do this:

Code:
            Set WBk2 = Workbooks.Open(Filename:=s)
 
Upvote 0
Ah thanks!
I tried dabbling with than and got an error, so I went back to what I knew.

Think it's because I did this:

VBA Code:
Set WBk2 = Workbooks.Open Filename:=s

Regardless, noted with thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

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