VBA to Paste Values

thewiseguy

Well-known Member
Joined
May 23, 2005
Messages
1,015
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Currently I am using the following VBA to copy from one workbook to another:

'Copy range to another workbook
Workbooks("Catalog.xlsm").Worksheets("Fixtures").Range("B4:AC1000").Copy("Proposal.xlsm").Worksheets("Fixtures").Range ("B4:AC1000")

How can i change this to paste values? I don't want the formats to come along into the other workbook.

Any help is greatly appreciated!
 
After you open the workbook you can save it with a different name. It is a better practice to use an object instead of repeating the name each time. Your code assumes that the proposal file is already open. I'll keep that assumption, but the code could open it for you.

Here is a rewrite of your code. You need to decide how you want to name the new copy. I have a placeholder for you.

Rich (BB code):
Sub Copy_Method()

   'Copy range to another workbook using Range.Copy Method\
   Dim Proposal As Workbook
   Dim Catalog As Workbook
   Dim SheetName As Variant
   
   Set Proposal = Workbooks("Proposal.xlsm")
   Set Catalog = Workbooks("Catalog.xlsm")
   
   Proposal.SaveAs Filename:="My New Proposal.xlsm"
   
   For Each SheetName In Array("EXS Catalog", "Fixtures", "Lamps", "Retrofit_Kits", "No_Measure", "Accessories", "Controls", "Materials", "Recycling", "Rental", "Labor")
      Proposal.Worksheets(SheetName).Range("B4:AD1000").Value = Catalog.Worksheets(SheetName).Range("B4:AD1000").Value
   Next SheetName

End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
After you open the workbook you can save it with a different name. It is a better practice to use an object instead of repeating the name each time. Your code assumes that the proposal file is already open. I'll keep that assumption, but the code could open it for you.

Here is a rewrite of your code. You need to decide how you want to name the new copy. I have a placeholder for you.

Rich (BB code):
Sub Copy_Method()

   'Copy range to another workbook using Range.Copy Method\
   Dim Proposal As Workbook
   Dim Catalog As Workbook
   Dim SheetName As Variant
 
   Set Proposal = Workbooks("Proposal.xlsm")
   Set Catalog = Workbooks("Catalog.xlsm")
 
   Proposal.SaveAs Filename:="My New Proposal.xlsm"
 
   For Each SheetName In Array("EXS Catalog", "Fixtures", "Lamps", "Retrofit_Kits", "No_Measure", "Accessories", "Controls", "Materials", "Recycling", "Rental", "Labor")
      Proposal.Worksheets(SheetName).Range("B4:AD1000").Value = Catalog.Worksheets(SheetName).Range("B4:AD1000").Value
   Next SheetName

End Sub

Hi I appreciate your efforts on this. I am a little confused. Let me try to explain exactly what I am trying to accomplish.

"Catalog" and "Proposal" both get opened at the same time. I input data into "proposal" and then save "proposal" as something else...i.e "ABC.Company.Proposal." A week later I need to revise this proposal so I open up ""ABC.Company.Proposal" and I need to re-run the copy VBA b/c I need to input some new products into this existing proposal. Currently if I re-run the copy VBA it will not work, so if possible, I'd like it to be able to copy the data from "Catalog" back into the proposal.

Does this make more sense?
 
Last edited:
Upvote 0
**edit - so I used the following in place of your placeholder:

Proposal.SaveAs FileName:=Worksheets("Dashboard").Range("F5")

It worked perfectly. However, here is the next problem. I don't know what products I am going to be adding to "Catalog" until I open "Proposal" and start inputting data. So in an example, both docs are opened, i start to input data, then I realize I need a new product. I enter in the new product, run the VBA and there is my new product. I keep inputting data and realize I need a new product, but now I can run the VBA again b/c the name has changed. I guess I could always save it back to "Proposal" but is this the easiest way?
 
Upvote 0
I am getting a little lost.

Are you saying that:

  1. You open Proposal.xlsm
  2. You save Proposal.xlsm as a new file, named by what's in cell F5 of...I don't know what. Which file contains sheet Dashboard? For now we'll call that file NewProp.xlsm.
  3. Then your code copies a bunch of stuff from workbook Catalog to NewProp.xlsm.
  4. All good.
  5. Whoops! Now you need to update the Catalog file, and you need to copy that to NewProp.xlsm. But your code wants to open the original Proposal.xlsm to copy the data to, not the new copy.

That's a design flaw in the way you have structured your code. You need to separate the code that creates the new file with the code that copies content to the new file, and make it two separate operations. If NewProp.xlsm is still open this is a little easier and you can refer to the file as Workbooks(Worksheets("Dashboard").Range("F5")). I would set a variable
VBA Code:
Dim NewProp As Workbook
Set NewProp = Workbooks(Worksheets("Dashboard").Range("F5"))
then the copy code can copy to NewProp.
 
Upvote 0
I am getting a little lost.

Are you saying that:

  1. You open Proposal.xlsm
  2. You save Proposal.xlsm as a new file, named by what's in cell F5 of...I don't know what. Which file contains sheet Dashboard? For now we'll call that file NewProp.xlsm.
  3. Then your code copies a bunch of stuff from workbook Catalog to NewProp.xlsm.
  4. All good.
  5. Whoops! Now you need to update the Catalog file, and you need to copy that to NewProp.xlsm. But your code wants to open the original Proposal.xlsm to copy the data to, not the new copy.

That's a design flaw in the way you have structured your code. You need to separate the code that creates the new file with the code that copies content to the new file, and make it two separate operations. If NewProp.xlsm is still open this is a little easier and you can refer to the file as Workbooks(Worksheets("Dashboard").Range("F5")). I would set a variable
VBA Code:
Dim NewProp As Workbook
Set NewProp = Workbooks(Worksheets("Dashboard").Range("F5"))
then the copy code can copy to NewProp.

hi again. i am getting a mismatch error on the 2nd line?
 
Upvote 0
Sub Copy_Method()
'Copy range to another workbook using Range.Copy Method

'Copy range to another workbook


Workbooks("Proposal.xlsm").Worksheets("EXS Catalog").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("EXS Catalog").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Fixtures").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Fixtures").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Lamps").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Lamps").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Retrofit_Kits").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Retrofit_Kits").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("No_Measure").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("No_Measure").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Accessories").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Accessories").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Controls").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Controls").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Materials").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Materials").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Recycling").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Recycling").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Rental").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Rental").Range("B4:AD1000").Value
Workbooks("Proposal.xlsm").Worksheets("Labor").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Labor").Range("B4:AD1000").Value


Dim NewProp As Workbook
Set Proposal.xlsm = Workbooks(Worksheets("Dashboard").Range("F5"))


End Sub
 
Upvote 0
Set Proposal.xlsm = Workbooks(Worksheets("Dashboard").Range("F5"))
Compare that line of the code to the line of code I gave you.

But that's also not how I intended for you to use it. You should be doing this at the top, and then referring to NewProp, not Workbooks("Proposal.xlsm").
 
Upvote 0
sorry, im new to VBA. Now what am I doing wrong? Still getting the same error.


VBA Code:
Sub Copy_Method()
'Copy range to another workbook using Range.Copy Method

'Copy range to another workbook

Dim NewProp As Workbook
Set NewProp = Workbooks(Worksheets("Dashboard").Range("F5"))


NewProp.Worksheets("EXS Catalog").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("EXS Catalog").Range("B4:AD1000").Value
NewProp.Worksheets("Fixtures").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Fixtures").Range("B4:AD1000").Value
NewProp.Worksheets("Lamps").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Lamps").Range("B4:AD1000").Value
NewProp.Worksheets("Retrofit_Kits").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Retrofit_Kits").Range("B4:AD1000").Value
NewProp.Worksheets("No_Measure").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("No_Measure").Range("B4:AD1000").Value
NewProp.Worksheets("Accessories").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Accessories").Range("B4:AD1000").Value
NewProp.Worksheets("Controls").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Controls").Range("B4:AD1000").Value
NewProp.Worksheets("Materials").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Materials").Range("B4:AD1000").Value
NewProp.Worksheets("Recycling").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Recycling").Range("B4:AD1000").Value
NewProp.Worksheets("Rental").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Rental").Range("B4:AD1000").Value
NewProp.Worksheets("Labor").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Labor").Range("B4:AD1000").Value





End Sub
 
Upvote 0
I thought this one would work but to no avail....

VBA Code:
Sub Copy_Method()
'Copy range to another workbook using Range.Copy Method

'Copy range to another workbook

Dim NewProp As Workbook
Set NewProp = Workbooks(Worksheets("Dashboard").Range("F5"))


Workbooks("NewProp.xlsm").Worksheets("EXS Catalog").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("EXS Catalog").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Fixtures").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Fixtures").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Lamps").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Lamps").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Retrofit_Kits").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Retrofit_Kits").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("No_Measure").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("No_Measure").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Accessories").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Accessories").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Controls").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Controls").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Materials").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Materials").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Recycling").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Recycling").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Rental").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Rental").Range("B4:AD1000").Value
Workbooks("NewProp.xlsm").Worksheets("Labor").Range("B4:AD1000").Value = Workbooks("Catalog.xlsm").Worksheets("Labor").Range("B4:AD1000").Value





End Sub
 
Upvote 0
Compare that line of the code to the line of code I gave you.

But that's also not how I intended for you to use it. You should be doing this at the top, and then referring to NewProp, not Workbooks("Proposal.xlsm").

Hi again - I don't need a code to change the name of the workbook. I am only looking for the copy code to work in whatever workbook I am working in (b/c the names are always different).

Many thanks if you can take a stab at this!
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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