Click here to Skip to main content
15,916,215 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating a macro that transfer data from one workbook to another. i am getting a error please help. i have taken command button on workbook on b.xls which will run this.



VB
Private Sub CommandButton1_Click()

Dim wkb1 As Workbook
Dim wknm As String
wknm = "d:\a.xlsx";
Set wkb1 = Workbooks.Open(wknm)
wkb1.Sheets("Sheet1").Range("A1:A8").Copy

ThisWorkbook.Sheets("Sheet1").Range("A1").Select
Selection.Paste

End Sub

Please help me find out what i am doing wrong
Thanks
aks619
Posted
Updated 22-Oct-12 21:58pm
v2
Comments
Zoltán Zörgő 23-Oct-12 3:55am    
You are getting error. Fine, and what error?
aksh619 23-Oct-12 3:57am    
Application Defined or Object defined error !!
Zoltán Zörgő 23-Oct-12 4:14am    
And you have done step-by-step debugging, and you reached witch code row?
Buy as I see, you can not copy a range. Select it first, than copy, than select the target and than paste (See msdn).
aksh619 23-Oct-12 4:20am    
now giving error select method of range class faild . i has put the whole code in Module .
Zoltán Zörgő 23-Oct-12 4:26am    
Workbook is opened?
Is there Sheet1?
Range is selected?

1 solution

This is working fine for me:
VB
Public Sub test()
Dim wb As Workbook
Set wb = Workbooks.Add
Workbooks(1).Activate
ActiveSheet.Range("A1").Value = 1
ActiveSheet.Range("A8").Value = 8
ActiveSheet.Range("A1:A8").Select
Selection.Copy
Workbooks(2).Activate
ActiveSheet.Range("A1").Select
ActiveSheet.Paste
End Sub
 
Share this answer
 
Comments
aksh619 23-Oct-12 4:52am    
Thanks Zoltan Zorgo, really appreciate you help .. but with one thing more how can i transfer entire coulmn to different workbook worksheet ..
Zoltán Zörgő 23-Oct-12 12:32pm    
Use the "A:A" format for one column or "A:C" for many. I suppose you can only select consequent columns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900