Why do I have to activate my worksheet for this to work?

Amileaux

Board Regular
Joined
Nov 3, 2002
Messages
110
Below is a portion of VB that I use in Access to populate an excel worksheet.
Everything works just fine until: wksRTPT.Range("A1").Select. I continually errored out (error 1004: select method of range class failed) UNTIL I added the line wksRTPT.Activate before it. Whey would the lines of code before work and this one little line not work until I activate the worksheet explicity? To update the pivot tables I had to implicitly be on that worksheet. I'm confused. Marie

Dim wkbTemplate As Excel.Workbook
Dim wksRTPT As Excel.Worksheet
Set wkbTemplate = GetObject(strFilePath & "DailyFlash.xlt")
Set wksRTPT = wkbTemplate.Worksheets("RTPT")

'Refresh Pivot Tables
For i = 1 To wksRTPT.PivotTables.Count
wksRTPT.PivotTables(i).RefreshTable
Next
wksRTPT.Cells.EntireColumn.AutoFit
wksRTPT.Columns("A:A").ColumnWidth = 15.57
wksRTPT.Activate
wksRTPT.Range("A1").Select
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I'm not sure of the reason why, but when I was build some MSAccess reports using the Excel object, I found that some of the syntax I use in the Excel Editor didn't work using the Excel Object. I espcially had trouble with my references. Don't remember the exact problems, it was a while ago.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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