Getting Range of Object - Worksheet Error in fully referenced code

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
176
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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,334
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You didn't qualify the Cells properties with the sheet:

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

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,334
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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)
 

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
176
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!
 

Forum statistics

Threads
1,141,048
Messages
5,703,928
Members
421,321
Latest member
blusky4

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
Top