Create an Array from different sheet without selecting the sheet

Matt_O

Board Regular
Joined
May 23, 2013
Messages
64
Hi Folks,

I've been searching for a while as to why I can't create an array from data on a specific worksheet without first selecting the sheet and haven't found a solution. So I'm back asking for help!

There's no trouble getting a Long variable from the non-selected sheet but the syntax for the array is proving hard to come by.

I am trying to populate an array from data in column A on a sheet called cnClients. cnClients is the code name for Worksheets("Clients").

The range is dynamic hence the need to first find the last row of data with the variable 'a'.

I've tried wrapping the creation of the array inside a With/End With statement and this does not throw an error but only creates an array with no data.

I get this error Method 'Range' of Object '_Worksheet' failed with the code below.

Code:
Option Explicit
Public client_array() As Variant


Sub clientList()


    Dim a As Long ' for counting rows in column A
    a = cnClients.Range("A65536").End(xlUp).Row


    client_array = cnClients.Range(Cells(2, 1), Cells(a, 1)).Value

End Sub

Any ideas?

THanks,
Matt
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You need to qualify the Cells reference. The way you have it, it's mixing references between cnClients and the active sheet. It should look more like this.

Code:
[COLOR=#333333]client_array = cnClients.Range([/COLOR][COLOR=#333333]cnClients.[/COLOR][COLOR=#333333]Cells(2, 1), [/COLOR][COLOR=#333333]cnClients.[/COLOR][COLOR=#333333]Cells(a, 1)).Value[/COLOR]
 
Upvote 0
Hi lrobbo314,

Thank you so much for the timely fix to my problem!

Much appreciated.

Cheers,
Matt
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,891
Members
449,194
Latest member
JayEggleton

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