Click here to Skip to main content
12,405,700 members (61,961 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: MS-Excel macros VBA
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.



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 22-Oct-12 21:50pm
aksh619297
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?
aksh619 23-Oct-12 4:28am
   
hey how can i go to different work book.... copy step is complete but

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

not working
aksh619 23-Oct-12 4:26am
   
how to move to different workbook after copying from first

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This is working fine for me:
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
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 23 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100