VBA copy data from a workbook to another

mark692

Active Member
Joined
Feb 27, 2015
Messages
321
Office Version
  1. 2016
Platform
  1. Windows
Good day sir i have two workbooks

1. workbookSource- where we will get the data

2. workbookPastehere- where the data will be pasted


what code will i use if i want data from workbookSource column A and Column D to be paste in workbookPastehere Sheet INPUT, column B and column F,

i start in using this VBA code

Sub CopyPaste()
Selection.copy
Workbooks.Open Filename:="C:\Users\Record\workbookPastehere.xlsm"
Sheets("Input").Select
Activesheet.paste
Application.cutcopymode = False


End sub

this code work if the workbookPastehere.xlsm is not yet open, problem occur when workbookPastehere.xlsm is already open an error "workbookPastehere.xlsm is already open. Reopening will cause". i want a code that will not reopen the workbookPastehere.xlsm if its already open. thanks :)
 
How about
Code:
Sub chkFileOpen()
   Dim wbk As Workbook
   Dim Fname As String
   Dim pth As String
   Dim Addr As String
   
   pth = "C:\Mrexcel\"
   Fname = "Book1.xlsm"
   Range("A:B").Copy
   Addr = Selection.Address
   If Not wbkExists(Fname) Then
      Set wbk = Workbooks.Open(pth & Fname)
   Else
      Set wbk = Workbooks(Fname)
   End If
   wbk.Sheets("Input").Range("A1").PasteSpecial

End Sub

Sub chkFileOpen()
Dim wbk As Workbook
Dim Fname As String
Dim pth As String
Dim Addr As String

pth = "C:\Mrexcel"
Fname = "Book1.xlsm"
Range("A2:A8").Copy
Addr = Selection.Address
If Not wbkExists(Fname) Then
Set wbk = Workbooks.Open(pth & Fname)
Else
Set wbk = Workbooks(Fname)
End If
wbk.Sheets("Sheet1").Range("D4").PasteSpecial



End Sub



i edit your code sir so i can use it on my workbook and it work, but my problem is after pasting the range "a2:a8" to cell D4, i want to go back in the orignal workbook and copy another range, i think we need a code after "wbk.Sheets("Sheet1").Range("D4").PasteSpecial" so i can go back in the source workbook


****** id="cke_pastebin" style="position: absolute; top: 632px; width: 1px; height: 1px; overflow: hidden; left: -1000px;"> wbk.Sheets("Sheet1").Range("D4").PasteSpecial</body>
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,819
Members
449,469
Latest member
Kingwi11y

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