Link to home
Start Free TrialLog in
Avatar of bcreen
bcreenFlag for United States of America

asked on

access 2013 vba code to select a named worksheet from an opened and activated workbook ?

The following code works fine in Access 2010 but gets a runtime error in 2013.
   xlWorkBookSalesPivot.Activate  ' works fine  in 2010 and 2013
   xlWorkBookSalesPivot.sheets("Pivot").Select

I tried various alternatives to the SELECT, including the below.  They get 'subscript out of range' errors:
   xlWorkBookSalesPivot.Activate  ' works fine  in 2010 and 2013
   xlExcelApp.worksheets("Pivot").Select

Has anyone got ideas on how to properly select that worksheet?  The 2010 code has run for 2 years, but we must update to run in Access 2013.
Thanks!
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

>>The following code works fine in Access 2010 but gets a runtime error in 2013.
   xlWorkBookSalesPivot.Activate  ' works fine  in 2010 and 2013
   xlWorkBookSalesPivot.sheets("Pivot").Select

so is these codes working fine in both 2010 and 2013 or just working fine in 2010?
if it got error in 2013, what's that error message received?

I tried various alternatives to the SELECT, including the below.  They get 'subscript out of range' errors:
   xlWorkBookSalesPivot.Activate  ' works fine  in 2010 and 2013
   xlExcelApp.worksheets("Pivot").Select
make sure that's a worksheet named as Pivot in the target workbook...?
Avatar of Norie
Norie

Why are you trying to activate/select the sheet?

You shouldn't need to.
how is xlWorkBookSalesPivot dimensioned and assigned? Would you please post the whole procedure? thanks
Avatar of bcreen

ASKER

Norie and Ryan,  thanks for your response.  There IS a worksheet existing -- and 2010 code has run flawlessly for 2+years.

The reason I'm trying to activate then select the particular sheet is so that I can then place some verbiage into 2 of the cells of that "Pivot" sheet:
    ' now place the NAME we are processing into A3
    xlExcelApp.worksheets("Pivot").Range("A3").Value = GetpLevel4Name()
    xlExcelApp.worksheets("Pivot").Range("A4").Value = GetpstrLevel()
    DoEvents: DoEvents

Without the SELECT clause (which fails in 2013) I get RTE 9 - subscript out of range trying to place a value into the "A3" cell in the statement above.

After placing some text into the 2 cells above I then run macro that are stored within the workbook itself:
        ' now run the macro in that workbook which clears filters and sets the filter for THIS L3 or L4 !
        xlExcelApp.Run "SALES_SetFilterTo"    ' it also DELETES a tab named "IndividualPivot" if it exists

I may try decompile, compact and repair, and moving all objects into a fresh accdb ..... (which I've done recently trying to get this program to work in AC2010).  But I doubt this will make much difference....

Thanks for your input!
Avatar of bcreen

ASKER

Sorry Crystal -- overlooked your question.  The code is for a major international company and I am unable to publicly publish much of the code or data without being in violation of my confidentiality agreement.   I'll review how I've dimensioned and defined the various objects.  But this code has run extremely well in AC2010, until file/table sizes have reached the point where 2010 is failing -- but 2013 handles the massive data fine.   'Rock and a hard place' scenario.....   :-)

I suppose this could also be related to Tools|References|libraries used and their order.....
What happens if you don't activate/select and replace xlExcelApp here with the appropriate workbook reference which I believe is xlWorkBookSalesPivot.
xlExcelApp.worksheets("Pivot").Range("A3").Value = GetpLevel4Name()

Open in new window

hi,

if you are simply changing values in cells, there is no reason to select anything. Sheets only need to be selected/activated if you are doing something like freezing panes or setting the zoom level

In addition to Norie's suggestion, I wish to add that you might think of dimensioning a variable, assigning it the variable the return value of GetpLevel4Name(), setting the cell value to the variable ... and testing somewhere along the way too -- what if there is no group level 4?

... however, would it not be better to put a .formula into the cell (as opposed to .value) in case the pivot table is changed? something like   =MyOtherSheet!A1 ? ... or ...

____________________________
to fix the main issue at hand, though ...

Most important is Ryan's observation of Worksheet vs Sheet.  A pivot table/chart is a sheet (in the Sheets collection), but it may not be in the Worksheets collection.  Same as a chart sheet is not a worksheet (according to object model) ... nor an old-time macro sheet, ....

Best to use the Sheets collection in code since it can refer to any type of sheet, whereas a Worksheet is a specific type of sheet.  Take a look here at the differences between a Sheet object and a Worksheet object:

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-object-excel

Worksheets Object   (collection)
https://msdn.microsoft.com/en-us/VBA/Excel-VBA/articles/worksheets-object-excel

Sheets Object (collection)
https://msdn.microsoft.com/en-us/VBA/Excel-VBA/articles/sheets-object-excel

have an awesome day,
crystal
Avatar of bcreen

ASKER

Great scoop here Crystal  -- time constraints often cause us to STOP after getting "something" that works, without actually furthering our actual understanding of what, exactly, we are really DOING -- and why.

Thanks you so much for taking your time to help me out here.  I'm beginning to suspect that "something"  elsewhere is causing what we are seeing in this section of the code.  But I'll try your suggestions and review your  links.

Thanks again!
Bill
ASKER CERTIFIED SOLUTION
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial