+ Reply to Thread
Results 1 to 3 of 3

Set array= range, and using a variable name for a worksheet

  1. #1
    Registered User
    Join Date
    11-17-2008
    Location
    WA
    Posts
    11

    Set array= range, and using a variable name for a worksheet

    I'm trying to do two things, and somewhere between them I'm doing something wrong.

    (1) I open an Excel file using the following code; I first set the active workbook to a variable, then open the second workbook and set it equal to another variable. This is so that I can reference each 'on the fly' using my code name instead of trying to figure out which one is active at any given time:

    'save the workbook and worksheet that was active when the code was called
    SummaryWB = Application.ActiveWorkbook
    SummaryWS = Application.ActiveWorkbook.ActiveSheet

    '{do other stuff- deleted to save space}

    Workbooks.Open Filename:=FName ', ReadOnly:=True
    DoEvents
    Set SourceWB = ActiveWorkbook

    'confirmed with a debug.print SourceWB.name - this shows the selected workbook name in the debug window

    (2) Then, I want to assign some worksheet ranges to some variables so I can use application.match against them. I know I've done this at a previous employer, but I can't get the syntax right, and I no longer have access to that code base. It should be something like:

    SourceList1 = SourceWB.Sheet1.Range("A1:A500").Value

    but after processing that line, the VBE tells me that SourceList1 is 'empty' (on mouseover). For some reason, it isn't filling setting my variable to that range. I've also tried variations such as

    Set SourceList1 = SourceWB.Sheet1.Range("A1:A500").Value
    Set SourceList1 = SourceWB.Sheet1.Range("A1:A500")
    SourceList1 = SourceWB.Sheet1.Range("A1:A500")

    but so far, nothing seems to work and I'm not sure if the problem is here, or my original assignment of the workbook name. Can anyone provide any hints or insights?

    Many thanks,
    Keith

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Keith

    I don't think it likes the sheet1 in this instance.

    Try nominating a specific sheet.

    Please Login or Register  to view this content.
    rylo
    Last edited by rylo; 11-17-2008 at 11:50 PM.

  3. #3
    Registered User
    Join Date
    11-17-2008
    Location
    WA
    Posts
    11
    Your observation was correct- that is where it was getting stuck- so I altered the code a little and just used my previously assigned variable to pull the range instead- so instead of

    Set SummaryWB = Application.ActiveWorkbook
    Set SummaryWS = Application.ActiveWorkbook.ActiveSheet
    SourceList1 = SourceWB.Sheet1.Range("A1:A500").Value

    I replaced that last line with
    SourceList1 = SummaryWS .Range("A1:A500").Value

    to eliminate the Sheet1 reference, and Woot! It is now pulling in the ranges as expected. I couldn't hardcode the visible sheetname because the users who create the files rename the sheet, so I wouldn't know what each one was in advance.

    Thanks Rylo!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1