Issue by using UsedRange for copying values from a worksheet and paste it into another workshet

fabro5

New Member
Joined
Sep 10, 2018
Messages
1
Hi,

I am struggling to do something really simple:
Adding a Macro to my active workbook which opens another workbook (located in the same folder), copy all the values from one sheet and pasting it into my active worksheet.

Here is my code:

Sub Button1_Click()


Dim filename As String
Dim nb_csp As String


Dim c As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet




nb_csp = Range("'5- AOCS'!K12")


filename = "" & nb_csp & "_" & "BOM.xls"


Set c = Workbooks.Open(ActiveWorkbook.Path & filename)


Set ws1 = c.Sheets("ModuleInfoDetail")
Set ws2 = ActiveSheet


ws1.UsedRange.Copy ws2.Cells("A5")




End Sub

I got an error: Run-time error '5':
Invalid procedure call or argument

Could somebody help me?

thank you very much in advance
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This
Code:
ws1.UsedRange.Copy ws2.Cells("A5")
should be
Code:
ws1.UsedRange.Copy ws2.Range("A5")
or
ws1.UsedRange.Copy ws2.Cells(5,1)
But I suspect that it still won't work properly, as I think that you have the wrong sheet for ws2
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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