Please explain why this simplest bit of VBA select doesn't work

Johnny C

Well-known Member
Joined
Nov 7, 2006
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Here's the code. There's 2 workbooks, the intention is to copy from one to the other. Both workbook names are strings. Both files have worksheets codenamed wksSourceData/wksTargetData (it doesn't make any odds if I change them).

But I'm getting 'Objest doesn't support this property or method' on either of the select statements (oddly sometimes it's the forst sometimes it's the second)

I've been using this code for years, what am I doing wrong? I think I'm going senile

VBA Code:
Workbooks(ThisFile).Activate
ActiveWorkbook.wksSourceData.Range("A5:A2000").Select
Selection.Copy
Workbooks(CopyFile).Activate
ActiveWorkbook.wksTargetData.Range("N4").Select
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It should be
VBA Code:
wksSourceData.Range("A5:A2000").Select
But that will refer to the sheet in the workbook that contains the code.
 
Upvote 0
Yes, that's the intention. There's a worksheet in the workbook that contains the code codenamed "wksSourceData"

Even this gives an error
VBA Code:
' First set this file as the source file
ThisFile = ActiveWorkbook.Name

Workbooks(ThisFile).Activate
ActiveWorkbook.wksSourceData.Select
 
Upvote 0
You still have ActiveWorkbook in there, get rid of it.
 
Upvote 0
It doesn't work without it either.

The only way I have been able to get round it is add a loop through all worksheets in both, if the sheet.codename = the one I want set a string to the sheet name.

I'm convinced whatever version of Excel 365 I'm on VBA is riddled with bugs. Things just don't work when they should. I grab bits of code from all sorts of places even stuff from Chandroo falls over. I spent 3 days last week trying to get VBA to set a pagefield multiple selection in a pivot table.
 
Upvote 0
As I said wksSourceData.Select will only work for the workbook that contains the code, also that workbook needs to be the active workbook.
If you are trying to access a sheet in anything other than "ThisWorkbook" you cannot directly use the code name, unless you set a reference to that workbook.
This is not a "bug" it is how it has always worked.

Also it is very rare that you need to select or activate anything.
 
Upvote 0
That was how I started off, using Set .

Even the simplest VBA won't work

VBA Code:
Sub TestSelect()
Dim ThisWB As Workbook

Set ThisWB = ActiveWorkbook
[B]ThisWB.wksSourceData.Range("A:A").Select[/B]
crashes Object doesn't support this property or method. It knows wksSourceData is there because it capitalises it.
 
Upvote 0
It was crashing on this earlier, strFile was not a valid datatype even though I could see in the Locals browser it was a variant/String.
Now its suddenly working and I haven't touched anything in it.
VBA Code:
Dim strFile As Variant, CopyFile$
strFile = Application.GetOpenFilename(FileFilter:="Excel files (*.xls*), *.xls*", Title:="Choose a TM1 Input file to open", MultiSelect:=False)
If strFile = False Then
    MsgBox "No file selected"
    Exit Sub
End If
Workbooks.Open Filename:=strFile
CopyFile = Right(strFile, Len(strFile) - InStrRev(strFile, "\"))
 
Upvote 0
You cannot do this ThisWB.wksSourceData it needs to be as I showed.
 
Upvote 0
And now application.ScreenUpdating no longer works. I will get in touch with our IT, something is very wrong.
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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