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
 
The code you originally posted is using the sheet codename in two different workbooks. You CANNOT do that unless you have specifically set a reference to the other workbook in the VB Editor (Not in the code)
 
Upvote 0
Solution

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Ah it need a tools/Reference? I'm sure I've never had to do that before. Unfortunately the user chooses the file and they're no VBA savvy so references are out.
I got around it by SET each workbook and then loop through worksheets until the codenames I wanted are found and set a string I can use sheets() with. Clumsy but it works.
Application.ScreenUpdating doesn't work either. I can't figure out why. One macro with it in works, one doesn't. I even copied the code from one to the other and it still doesn't work.
 
Upvote 0
Glad you've sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,543
Messages
6,125,423
Members
449,223
Latest member
Narrian

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