Open new workbook and paste to it

EOAEvan

Active Member
Joined
Sep 20, 2007
Messages
399
I have a line of VBA code Workbooks.add to add a new book. What I want to accompish is to copy a sheet from another existing book (where the code is located) to the new workbook. Though the new workbook number can change. How can I get it to paste in the new book reguardless of the number its assigned?
 

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.
Hi,

When you add the workbook it should become the active one so maybe:

Code:
Sub Copy_Sheet()
    
Workbooks.Add
ThisWorkbook.Sheets("Sheet2").Copy Before:=ActiveWorkbook.Sheets(1)
    
End Sub

Hope it helps,

Dom
 
Upvote 0
You will need to learn a little about qualified references:). Here's the base structure you will need to use.

Code:
Dim wb As Workbook, ws As Worksheet
Set wb = ActiveWorkbook
Set ws = ActiveSheet

Set pb = Workbooks.Add
Set ps = ActiveSheet

ws.Range("A1:B6").Copy ps.Range("A1")

Since you set the pb reference in the .add statement, it doesn't matter what the name of the file is.

HTH
Cal
 
Upvote 0
Thanks for the responces. both seem to work. here is what i have so far. New problem is that after i paste as value its changeing a long account number from 110240070000000 to 1.1024E+14. I need it to remain in the orginal format that shows the entire number.

Im sure there are better ways to do this. What I have works but if you want to tackle improving whats here im always up for learning a new/better way.

Code:
Sub HardCode()
Application.ScreenUpdating = False
Workbooks.Add
ThisWorkbook.Sheets("TOTALS").Copy Before:=ActiveWorkbook.Sheets(1)
ThisWorkbook.Sheets("Central").Copy after:=ActiveWorkbook.Sheets("Totals")
ThisWorkbook.Sheets("Florida").Copy after:=ActiveWorkbook.Sheets("Central")
ThisWorkbook.Sheets("South").Copy after:=ActiveWorkbook.Sheets("Florida")
ThisWorkbook.Sheets("West").Copy after:=ActiveWorkbook.Sheets("South")
ThisWorkbook.Sheets("East").Copy after:=ActiveWorkbook.Sheets("West")

ActiveWorkbook.Sheets("sheet1").Delete
ActiveWorkbook.Sheets("sheet2").Delete
ActiveWorkbook.Sheets("sheet3").Delete

ActiveWorkbook.Sheets("TOTALS").Range("a19:f30").Value = ActiveWorkbook.Sheets("TOTALS").Range("a19:f30").Value
ActiveWorkbook.Sheets("Central").Range("a24:f500").Value = ActiveWorkbook.Sheets("Central").Range("a24:f500").Value
ActiveWorkbook.Sheets("Florida").Range("a24:f500").Value = ActiveWorkbook.Sheets("Florida").Range("a24:f500").Value
ActiveWorkbook.Sheets("South").Range("a24:f500").Value = ActiveWorkbook.Sheets("South").Range("a24:f500").Value
ActiveWorkbook.Sheets("West").Range("a24:f500").Value = ActiveWorkbook.Sheets("West").Range("a24:f500").Value
ActiveWorkbook.Sheets("East").Range("a24:f500").Value = ActiveWorkbook.Sheets("East").Range("a24:f500").Value

ActiveWorkbook.Sheets("Totals").Range("F30") = ("=sum(F19:F29)")

ActiveWorkbook.Sheets("totals").Select
Range("a1").Select
Application.ScreenUpdating = True

End Sub


Thanks!
 
Upvote 0
Why not just create a new workbook with the required worksheets rather than a blank one?
Code:
ThisWorkbook.Sheets(Array("TOTALS", "Central", "Florida", "South", "West", "East")).Copy
 
Upvote 0
Thanks Norie. That will help shorten the code. Though the actual format issue is still there. I know i can fix this by changing the format of the ranges but the only way i know how is to use .select and from reading there is always a better way than range.select etc.
 
Upvote 0
Sorry I don't quite understand.:confused:

The code I posted shouldn't change any formatting.
 
Upvote 0
That line of code doesnt. It "copies/pastes" exactly what is on the ThisWorkbook. Though there are many formulas that i need to change to values only. When I change them to values using
Code:
ActiveWorkbook.Sheets("TOTALS").Range("a19:f30").Value = ActiveWorkbook.Sheets("TOTALS").Range("a19:f30").Value
it loses the formatting for some reason.

The way i have it posted above acutally keeps the formatting up until i change that range to values only.
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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