vba - copy range from workbook to another

Tornado1981

Board Regular
Joined
Apr 1, 2010
Messages
248
hi,

i need a vba code to copy a range (A1:A10) from a closed workbook "Workbook1" and paste its value only without formats into an open workbook (Active) "Workbook2" , then close "Workbook1" without saving.

any help lease ?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
See if this works for you...

Code:
Sub CopyData()
Dim ws As Worksheet
Dim DataFile As String
   
    Set ws = ActiveSheet
    MsgBox ("Please select a file to copy data from.")
    DataFile = Application.GetOpenFilename("Excel Files(*.xls; *.xlsx; *.xlsm), *.xls; *.xlsx; *.xlsm", 1, "Select One File To Open", , False)
    If DataFile <> "False" Then
        Workbooks.Open DataFile, UpdateLinks:=False
        Range("A1:A10").Copy
        ws.Range("A1").PasteSpecial xlPasteValues
        ActiveWorkbook.Close False
    End If
End Sub
 
Upvote 0
hi sfpowell,
thanks for help
but why did u assume that i need to open a dialogbox to choose the workbook i want to copy cells from ??
this is my code. but when running it i got an error about the last line of the code but i don't know what's wrong with it

Code:
Private Sub CommandButton1_Click()
Dim wbk As Workbook
Dim filename As String
Dim sheetname As String
filename = "C:\Workbook1.xls"
sheetname = "Sheet1"
Set wbk = Workbooks.Open(filename)
With wbk.Worksheets(sheetname)
Range("A1:A10").Copy
End With
ActiveSheet.Range("B1:B10").Paste
End Sub
 
Upvote 0
hi,

i need a vba code to copy a range (A1:A10) from a closed workbook "Workbook1" and paste its value only without formats into an open workbook (Active) "Workbook2" , then close "Workbook1" without saving.

any help lease ?


Sub OpenCWB()


Dim WSD As Worksheet
Dim WB As Workbook
Set WB = ThisWorkbook
Set WSD = WB.Worksheets(1)
Workbooks.Open Filename:="C:\test.xlsx"
ActiveWorkbook.Worksheets(1).Range("A1:A10").Copy WSD.Range("A1")
ActiveWorkbook.Close savechanges:=False


End Sub
 
Upvote 0
why did u assume that i need to open a dialogbox to choose the workbook i want to copy cells from ??

Not knowing your skill level or the where the workbook was or the name of the worksheet, the dialog box seemed like the easiest solution that would work without modification. If the workbook and worksheet never changes, hardcoding is fine.

but when running it i got an error about the last line of the code but i don't know what's wrong with it

For your paste line you only need the specify the top cell you are pasting to and it needs to be a .PasteSpecial xlPasteValues so that it only copies the values and not the formats like you originally requested. Also the way you have your code written it doesn't close Workbook1 and it would actually paste to the same sheet in Workbook1. You need to specify that you are pasting to the original worksheets so I've added a variable for that. Try this code:

Code:
Private Sub CommandButton1_Click()Dim wbk As Workbook
Dim wsOrg As Worksheet
Dim FileName As String
Dim SheetName As String
    Set wsOrg = ActiveSheet
    FileName = "C:\Test\Workbook1.xls"
    SheetName = "Sheet1"
    Set wbk = Workbooks.Open(FileName)
    wbk.Worksheets(SheetName).Range("A1:A10").Copy
    wsOrg.Range("B1").PasteSpecial xlPasteValues
    ActiveWorkbook.Close False
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,273
Members
448,559
Latest member
MrPJ_Harper

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