Copy from workbook to workbook wlth VBA

rspalding

Active Member
Joined
Sep 4, 2009
Messages
282
Office Version
  1. 365
Platform
  1. Windows
I need help copying from 1 workbook to another. I want this code to read cell C1 for the wokbook name to open and then copy a list of cells from sheet 1 into the same cells sheet 1 in a different workbook identified by C5.

Thanks for your help,

Robert
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
John,

Ok I've tried to modify it but apparently ive done it incorrectly.

I want to copy A1

<TABLE style="WIDTH: 220pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=293 x:str><COLGROUP><COL style="WIDTH: 83pt; mso-width-source: userset; mso-width-alt: 4022" width=110><COL style="WIDTH: 137pt; mso-width-source: userset; mso-width-alt: 6692" width=183><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 83pt; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17 width=110>Folder:</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; WIDTH: 137pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" width=183>S:\JOB TICKETS</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>Filename:</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">10544-BENNY.xls\Ticket</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" height=17>worksheet</TD><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8">SHEET2
</TD></TR></TBODY></TABLE>

Here are the changes i made:

Sub CopySales()
Dim OrigWB As String, SourceWS As String
Dim lrow As Integer, lcol As Integer
'Assign variables
OrigWB = ActiveWorkbook.Name
SourcePath = Sheets("Main").Range("B1")
SourceWB = Sheets("Main").Range("B2")
TargetWS = Sheets("Main").Range("B3")
'Open WorkBook
With Workbooks.Open(SourcePath & "\" & SourceWB)
'Assign variables from Source Workbook
SourceWS = ActiveSheet.Name
Sheets(SourceWS).Range("A1").Copy
'Copy Source data to Target
Workbooks(OrigWB).Sheets(TargetWS).Range ("A1")
'Close Workbook
.Close
End With
End Sub

This won't change shhets in the opened workbbok and it won't paste the copy in "sales A1"

Thanks for your help.

Robert
 
Upvote 0
Robert

It might help if you clarified exactly what you want to copy, where you want to copy it from and where you want to copy it to.

I know you want to use information from a worksheet, but it might be better to first sort that out.

Then we should be able to alter things to use the values from the worksheet, which should be pretty straightforward.:)
 
Upvote 0
Norie,

Thanks for helping me. I want to open a workbook and go to a sheet called "estimate" and copy various cells and paste them into a workbook with a sheet called "estimate" also. Both workbooks have seveal sheets in them so I must point to the proper sheet.

Thanks,

Robert
 
Upvote 0
Robert

That's useful but still not quite enough information to give a full and specific answer.

Here's some general code.
Code:
Option Explicit
Sub CopyEstimate()
Dim wbOpen As Workbook
Dim wbThis As Workbook
    Set wbThis = ThisWorkbook
    
    Set wbOpen = Workbooks.Open("C:\TheOtherWB.xls")
    
    wbOpen.Worksheets("Estimate").Range("A1:C10").Copy wbThis.Worksheets("Estimate").Range("A1")
    
    wbOpen.Close
    
End Sub
The workbook/sheet names, path, range etc are hardcoded but they could easily be replaced with cell references.

To do that I think we would need to know exactly, eg workbook/sheet, that information is going to come from.:)

PS Oh, and it assumes you are copying from the opened workbook to the workbook the code is in.
 
Upvote 0
Norie,

No the workbook is not open and will be differnt every time. Also the work sheet pasted into will be open. I have to copy specific cells and not the entire sheet. So here is an example:

From -

Location: S:\JOB TICKETS

Workbook Name: 10544-BENNY.xls

Sheet: "Estimate"

To -

Location: Activeworkbook

Sheet: "Estimate"


Copy Cell G12 to G12; J17:AF21 and so on.
 
Upvote 0
Robert

I know the workbook isn't open - I didn't ask if it was or mention if it was.:)

Again the information you've posted is useful but it's still missing the most important part - where is the information about filename etc located?
 
Upvote 0
Norie,

I'm sorry if I'm giving you the information needed. I believe thais is what you want.

Location: S:\JOB TICKETS

Workbook Name: 10544-BENNY.xls


Sheet: "Estimate"
 
Upvote 0
And forgive my spelling too. I blame it on my fingers not doing what I tell them to.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,178
Members
448,871
Latest member
hengshankouniuniu

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