Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Macro independent of newbook name

I have a macro which should copy one sheet to a new book and then I
jump back and copy another sheet to the same new book. My problem how
do I make my macro independent of file names of the file I am copying
from and to?

I have called the file I am copying from ThisWorkbook but what should
I call the file I am copying to, mentioned as "book1" in the macro
below:

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Select
Selection.Copy
Windows("book1").Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

Can someone help?

From
HA14

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Macro independent of newbook name

If you are always going to copy the sheets to a new workbook, create a
workbook object. Then your code becomes this:

Dim wkbNewBook As Workbook
Set wkbNewBook = Application.Workbooks.Add

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Select
Selection.Copy
wkbNewBook.Activate
Sheets("Sheet1").Select
ActiveSheet.Paste


HTH,
Pflugs

"HA14" wrote:

I have a macro which should copy one sheet to a new book and then I
jump back and copy another sheet to the same new book. My problem how
do I make my macro independent of file names of the file I am copying
from and to?

I have called the file I am copying from ThisWorkbook but what should
I call the file I am copying to, mentioned as "book1" in the macro
below:

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Select
Selection.Copy
Windows("book1").Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

Can someone help?

From
HA14


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Macro independent of newbook name

Thank you, this did some of it but unfortunately not all. Can you help
me one more time? I want to copy three different sheets from this
workbook to the same new workbook. The problem is now first sheet
copies into one new workbook but the two other sheets copy into a
second new workbook - how should I fix this?

Dim wkbNewBook As Workbook
Set wkbNewBook = Application.Workbooks.Add

Range("A1:U58").Select
Selection.Copy
Workbooks.Add Template:="Workbook"
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Select
Selection.Copy
wkbNewBook.Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("SOL kvt").Select
Range("A1:U30").Select
Selection.Copy
wkbNewBook.Activate
Sheets("Sheet3").Select
ActiveSheet.Paste

Great if you can help again.
HA14

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Macro independent of newbook name

Just add a second workbook object. Then copy the second two ranges to that
workbook.

Dim wkbNewBook As Workbook
Set wkbNewBook = Application.Workbooks.Add
Dim wkbNewBook2 as workbook
set wkbNewBook2 = application.workbooks.add

Range("A1:U58").Copy
wkbNewBook.sheets(1).activate
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Copy
wkbNewBook.Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("SOL kvt").Select
Range("A1:U30").Copy
wkbNewBook2.Activate
Sheets("Sheet3").Select
ActiveSheet.Paste

Hope that helps,
Pflugs

"HA14" wrote:

Thank you, this did some of it but unfortunately not all. Can you help
me one more time? I want to copy three different sheets from this
workbook to the same new workbook. The problem is now first sheet
copies into one new workbook but the two other sheets copy into a
second new workbook - how should I fix this?

Dim wkbNewBook As Workbook
Set wkbNewBook = Application.Workbooks.Add

Range("A1:U58").Select
Selection.Copy
Workbooks.Add Template:="Workbook"
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Select
Selection.Copy
wkbNewBook.Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("SOL kvt").Select
Range("A1:U30").Select
Selection.Copy
wkbNewBook.Activate
Sheets("Sheet3").Select
ActiveSheet.Paste

Great if you can help again.
HA14


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Macro independent of newbook name

Thank you for the coding, just want to let you know I want it all
copied to the same new workbook and therefore the macro now reads:

Dim wkbNewBook As Workbook
Set wkbNewBook = Application.Workbooks.Add

Range("A1:U58").Copy
wkbNewBook.Sheets(1).Activate
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Copy
wkbNewBook.Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("SOL kvt").Select
Range("A1:U30").Copy
wkbNewBook.Activate
Sheets("Sheet3").Select
ActiveSheet.Paste

Hmm, I keep having the problem that nothing is actually copied from
the first sheet which I cannot figure out why since the area A1:U58 is
correct. The two other sheets copy in fine to the new workbook -
strange....

Thanks again
HA14



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Macro independent of newbook name

How about trying:

Range("A1:U58").Copy
wkbNewBook.Activate
Sheets(1).Select
ActiveSheet.Paste

Pflugs

"HA14" wrote:

Thank you for the coding, just want to let you know I want it all
copied to the same new workbook and therefore the macro now reads:

Dim wkbNewBook As Workbook
Set wkbNewBook = Application.Workbooks.Add

Range("A1:U58").Copy
wkbNewBook.Sheets(1).Activate
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("A&S kvt").Select
Range("A1:U56").Copy
wkbNewBook.Activate
Sheets("Sheet2").Select
ActiveSheet.Paste

ThisWorkbook.Activate
Sheets("SOL kvt").Select
Range("A1:U30").Copy
wkbNewBook.Activate
Sheets("Sheet3").Select
ActiveSheet.Paste

Hmm, I keep having the problem that nothing is actually copied from
the first sheet which I cannot figure out why since the area A1:U58 is
correct. The two other sheets copy in fine to the new workbook -
strange....

Thanks again
HA14


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Independent column widths Kathleen Excel Worksheet Functions 2 August 22nd 06 05:47 PM
Independent Lists... Corey New Users to Excel 2 July 10th 06 07:17 PM
independent macros XCESIV[_14_] Excel Programming 5 May 23rd 06 04:44 AM
Run a .xla as a independent application triki Excel Programming 3 August 12th 05 05:15 PM
can one cell contain more than one independent number John K Excel Discussion (Misc queries) 2 July 2nd 05 03:28 AM


All times are GMT +1. The time now is 07:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"