Copy from workbook to workbook wlth VBA

rspalding

Active Member
Joined
Sep 4, 2009
Messages
274
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
 

rspalding

Active Member
Joined
Sep 4, 2009
Messages
274
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
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.:)
 

rspalding

Active Member
Joined
Sep 4, 2009
Messages
274
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
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.
 

rspalding

Active Member
Joined
Sep 4, 2009
Messages
274
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
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?
 

rspalding

Active Member
Joined
Sep 4, 2009
Messages
274
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"
 

rspalding

Active Member
Joined
Sep 4, 2009
Messages
274
And forgive my spelling too. I blame it on my fingers not doing what I tell them to.
 

Forum statistics

Threads
1,082,119
Messages
5,363,268
Members
400,723
Latest member
Jsdk

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top