Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Change workbook name to a generic name

Hi,
I have a problem naming workbooks. I have a macro that if it runs, it will
create a new woorkbook and then the macro will look at the new workbook and
will run some other code. The problem is that I name the new workbook
"Book1". So if I run the macro again it will not work because Excel will open
a new workbook but it will be "book2". Is there a way to change my macro so
that it will look at the new workbook if it is "book1" or "book2" or "book3"
etc?

Thanks
Miguel
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Change workbook name to a generic name

By the way here's my code:
Application.Goto Reference:="INPUT"
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Application.CutCopyMode = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "input"
Range("D1:D3").Select
Range("D3").Activate
Windows("Test QPS Output for ADF Calc.xls").Activate
Range("C61").Select
Application.Goto Reference:="SUMMARYREPORT"
Selection.Copy
Windows("Book1").Activate
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "summary"
Range("A1").Select
End Sub

The part that says:
Windows("Book1").Activate

That's where I have the problem.
Thanks


"Miguel" wrote:

Hi,
I have a problem naming workbooks. I have a macro that if it runs, it will
create a new woorkbook and then the macro will look at the new workbook and
will run some other code. The problem is that I name the new workbook
"Book1". So if I run the macro again it will not work because Excel will open
a new workbook but it will be "book2". Is there a way to change my macro so
that it will look at the new workbook if it is "book1" or "book2" or "book3"
etc?

Thanks
Miguel

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Change workbook name to a generic name

It is always better to reference the workbook object..especially when you
work with multiple workbooks at a time...Try the below code.



Sub Macro()
Dim wb As Workbook

Application.Goto Reference:="INPUT"
Selection.Copy
Set wb = Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Application.CutCopyMode = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
wb.Sheets("Sheet1").Select
wb.Sheets("Sheet1").Name = "input"
Range("D1:D3").Select
Range("D3").Activate
Windows("Test QPS Output for ADF Calc.xls").Activate
Range("C61").Select
Application.Goto Reference:="SUMMARYREPORT"
Selection.Copy
wb.Activate
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "summary"
Range("A1").Select
End Sub

--
If this post helps click Yes
---------------
Jacob Skaria


"Miguel" wrote:

By the way here's my code:
Application.Goto Reference:="INPUT"
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Application.CutCopyMode = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "input"
Range("D1:D3").Select
Range("D3").Activate
Windows("Test QPS Output for ADF Calc.xls").Activate
Range("C61").Select
Application.Goto Reference:="SUMMARYREPORT"
Selection.Copy
Windows("Book1").Activate
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteColumnWidths
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "summary"
Range("A1").Select
End Sub

The part that says:
Windows("Book1").Activate

That's where I have the problem.
Thanks


"Miguel" wrote:

Hi,
I have a problem naming workbooks. I have a macro that if it runs, it will
create a new woorkbook and then the macro will look at the new workbook and
will run some other code. The problem is that I name the new workbook
"Book1". So if I run the macro again it will not work because Excel will open
a new workbook but it will be "book2". Is there a way to change my macro so
that it will look at the new workbook if it is "book1" or "book2" or "book3"
etc?

Thanks
Miguel

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Change workbook name to a generic name

Hi

Assign the new workbook to an object variable:

Set wbB=workbooks.add

then use the variable for reference. With your code it could look like this:

Dim wbA As Workbook
Dim wbB As Workbook
Dim DestSh As Worksheet

Set wbA = ThisWorkbook

Range("Input").Copy
Set wbB = Workbooks.Add
Set DestSh = wbB.Worksheets("Sheet1")
With DestSh.Range("A1")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteColumnWidths
End With

With DestSh.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
DestSh.PageSetup.PrintArea = ""
With DestSh.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
DestSh.Name = "input"
'Range("D1:D3").Select
DestSh.Range("D3").Activate

wbA.Activate

Range("SUMMARYREPORT").Copy
Set DestSh = wbB.Worksheets("Sheet2")
With DestSh.Range("A1")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteColumnWidths
End With
Application.CutCopyMode = False
With DestSh.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
DestSh.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
DestSh.Name = "summary"


Regards,
Per

"Miguel" skrev i meddelelsen
...
Hi,
I have a problem naming workbooks. I have a macro that if it runs, it will
create a new woorkbook and then the macro will look at the new workbook
and
will run some other code. The problem is that I name the new workbook
"Book1". So if I run the macro again it will not work because Excel will
open
a new workbook but it will be "book2". Is there a way to change my macro
so
that it will look at the new workbook if it is "book1" or "book2" or
"book3"
etc?

Thanks
Miguel


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Change workbook name to a generic name

It worked. Thanks a lot

Miguel

"Per Jessen" wrote:

Hi

Assign the new workbook to an object variable:

Set wbB=workbooks.add

then use the variable for reference. With your code it could look like this:

Dim wbA As Workbook
Dim wbB As Workbook
Dim DestSh As Worksheet

Set wbA = ThisWorkbook

Range("Input").Copy
Set wbB = Workbooks.Add
Set DestSh = wbB.Worksheets("Sheet1")
With DestSh.Range("A1")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteColumnWidths
End With

With DestSh.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
DestSh.PageSetup.PrintArea = ""
With DestSh.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
DestSh.Name = "input"
'Range("D1:D3").Select
DestSh.Range("D3").Activate

wbA.Activate

Range("SUMMARYREPORT").Copy
Set DestSh = wbB.Worksheets("Sheet2")
With DestSh.Range("A1")
.PasteSpecial Paste:=xlPasteValues
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteColumnWidths
End With
Application.CutCopyMode = False
With DestSh.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
DestSh.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperLetter
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
DestSh.Name = "summary"


Regards,
Per

"Miguel" skrev i meddelelsen
...
Hi,
I have a problem naming workbooks. I have a macro that if it runs, it will
create a new woorkbook and then the macro will look at the new workbook
and
will run some other code. The problem is that I name the new workbook
"Book1". So if I run the macro again it will not work because Excel will
open
a new workbook but it will be "book2". Is there a way to change my macro
so
that it will look at the new workbook if it is "book1" or "book2" or
"book3"
etc?

Thanks
Miguel



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
Any way to change generic icons in quick access toolbar? Marti Excel Worksheet Functions 6 July 7th 09 07:51 PM
Change a specific code to generic in VB macro Sue Excel Discussion (Misc queries) 2 April 23rd 08 06:56 PM
Generic Window/Workbook text jase[_2_] Excel Programming 2 December 28th 05 01:57 PM
Generic Workbook Select Michael Excel Programming 2 June 3rd 04 08:49 PM
Generic ComboBox change event steve Excel Programming 7 October 14th 03 07:30 PM


All times are GMT +1. The time now is 04:20 PM.

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

About Us

"It's about Microsoft Excel"