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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Domski

Well-known Member
Joined
Jan 18, 2005
Messages
7,292
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

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
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

EOAEvan

Active Member
Joined
Sep 20, 2007
Messages
399
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

Norie

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

EOAEvan

Active Member
Joined
Sep 20, 2007
Messages
399
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

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Sorry I don't quite understand.:confused:

The code I posted shouldn't change any formatting.
 
Upvote 0

EOAEvan

Active Member
Joined
Sep 20, 2007
Messages
399
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,191,383
Messages
5,986,307
Members
440,017
Latest member
vasanrajeswaran

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
Top