Error-438: Object doesn't support this property or method

zraza

New Member
Joined
Feb 7, 2017
Messages
10
Can someone advise what is the mistake in this code. I am having error on bold line. It says;

Error-438: Object doesn't support this property or method.

Code:
Option Explicit


Sub OpenAndCopy()


Dim FolderPath As String
Dim SourceFile As String
Dim TargetWB As String
Dim SourceWB As Workbook
Dim SourceWS As Worksheet


FolderPath = "C:\Users\admin\Downloads\Test"
SourceFile = Dir(FolderPath & "*.xlsx*")
TargetWB = ActiveWorkbook.Name


Set SourceWB = Workbooks.Open(FolderPath & SourceFile)
Set SourceWS = SourceWB.Worksheets("RRSP")


[B]SourceWS.Range("A:P").Copy_[/B]
Workbooks(TargetWB).Sheets("Sheet1").Range ("A1")


End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to the forum.

Remove the underscore character on the end of Copy.

PS Please don't post the same question more than once, and start your own thread rather than adding on to an unrelated question. I moved your question to a new thread for you this time (and deleted your duplicate post).
 
Upvote 0
Hi Rory,

I tried but it didn't work. It gives the same error again.

Btw sorry about posting it to an older thread as I am new to this form and not acquainted to it.
 
Upvote 0
couple of points

This line will need a backslash after the word Test
Code:
FolderPath = "C:\Users\admin\Downloads\Test"

This line doesn't need a wildcard after xlsx and without doing a search in the diectory, you will need to specify the filename, not use a wildcard

Code:
SourceFile = Dir(FolderPath & "*.xlsx*")
 
Upvote 0
couple of points

This line will need a backslash after the word Test
Code:
FolderPath = "C:\Users\admin\Downloads\Test"

This line doesn't need a wildcard after xlsx and without doing a search in the diectory, you will need to specify the filename, not use a wildcard

Code:
SourceFile = Dir(FolderPath & "*.xlsx*")

Basically, I want to open 10 to 12 files in a folder and copy data from them into my Active Workbook. I will have to use loop, but since I am new to VBA so I am struggling. This is my code. It would be great if you can help me figure it out.

Option ExplicitSub OpenAndCopy()

Dim FolderPath As String
Dim SourceFile As String
Dim TargetWB As String
Dim SourceWB As Workbook
Dim SourceWS As Worksheet

FolderPath = "P:\Macro\Test"
TargetWB = ActiveWorkbook.Name

SourceFile = Dir(FolderPath & "*.xls*")

Do While SourceFile <> ""

Set SourceWB = Workbooks.Open(FolderPath & SourceFile)
Set SourceWS = SourceWB.Worksheets("JE")

SourceWS.Range("A:P").Copy Workbooks(TargetWB).Sheets("Sheet1").Range("A1")

SourceWB.Close SaveChanges:=False
SourceFile = Dir()

Loop
End Sub
 
Upvote 0
My objective is to open 10 to 12 files in a folder one-by-one and copy data from them into another workbook. This is the code that I have written but I am having this error on the bold line of code.

Run-time error 1004:
Method 'Open' of object 'Workbooks' failed.

My code also copies data from first file into the Target workbook, and asks me if I want to re-open my Target workbook.

Any help would be appreciated.

CODE:

Sub OpenAndCopy()
Dim FolderPath As String
Dim SourceFile As String
Dim TargetWB As Workbook
Dim SourceWB As Workbook
Dim SourceWS As Worksheet

FolderPath = "P:\Macro\Test"
Set TargetWB = ActiveWorkbook

SourceFile = Dir(FolderPath & "*.xls*")
Do While SourceFile <> ""

Set SourceWB = Workbooks.Open(FolderPath & SourceFile)

Set SourceWS = SourceWB.Worksheets("JE")

SourceWS.Range("A:P").Copy TargetWB.Sheets("Sheet1").Range("A1")

SourceWB.Close SaveChanges:=False
SourceFile = Dir()

Loop
End Sub
 
Upvote 0
Don't have Excel at the moment, so this is UNTESTED


Code:
Sub MM1()
Dim Path As String
Dim SourceFile As String
Dim TargetWB As Workbook
Dim SourceWB As Workbook
Dim SourceWS As Worksheet

Path = "P:\Macro\Test"
Set TargetWB = ActiveWorkbook
SourceFile = Dir(Path & "*.xls*")
Do While SourceFile <> ""
    Set SourceWB = Workbooks.Open(FolderPath & SourceFile)
    Set SourceWS = SourceWB.Worksheets("JE")
    SourceWS.Range("A:P").Copy TargetWB.Sheets("Sheet1").Range("A1")
    SourceWB.Close SaveChanges:=False
    SourceFile = Dir()
Loop
End Sub
 
Upvote 0
Where do you want the copied data from each workbook to go?
 
Upvote 0
It didn't work and I am having the same error;

Run-time error 1004:
Method 'Open' of object 'Workbooks' failed.


Any suggestions to resolve it?
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,187
Members
449,090
Latest member
bes000

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