Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JER JER is offline
external usenet poster
 
Posts: 2
Default Naming convention for a variable "Book1" while copying sheets

I have gotten this far in developing a macro for saving
sheets to a new workbook that users will name themselves.
I can not count on the new workbook to be "Book1" all the
time as the user may have one or more new Books (Book1-n)
open and the next book number would be book1-n+1.

So I need to somehow establish a convention to assign a
name to the new book in the following code (in earlier
versions of this saga I was copying only the input cells
to a new book and the solution there was to simply
use "Windows(2)" as the second book. That doesn't work in
this case.)


Sub sheetsavetrialz()
'
'This starts the save process which saves all cells with
required or optional user input in the selected areas to a
new workbook.
'establishes "ps" as the name for the active workbook
ps = ThisWorkbook.Name
Windows(ps).Activate
ActiveWindow.WindowState = xlMinimized

Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=Workbooks
("Book1").Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=Workbooks("Book1").Sheets(1)
....
and on until the desired sheets (selected via a control)
are saved.



THANKS!
JER



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Naming convention for a variable "Book1" while copying sheets

When you execute the Add method of the Workbooks collection, set a variable to its return value. Then you can keep the workbook object in memory, and refer to it that way

Original code

Workbooks.Ad
Windows("ps").Activat
Sheets("Environment").Selec
Sheets("Environment").Copy Befo=Workbooks("Book1").Sheets(1
Windows("ps").Activat
Sheets("CT").Selec
Sheets("CT").Copy Befo=Workbooks("Book1").Sheets(1

Recommended changes

dim wbkNew as Workboo
set wbkNew = Workbooks.Ad
Windows("ps").Activat
Sheets("Environment").Selec
Sheets("Environment").Copy Befo=wbkNew.Sheets(1
Windows("ps").Activat
Sheets("CT").Selec
Sheets("CT").Copy Befo=wbkNew.Sheets(1

And consider this code to eliminate the whole "Select" and "Activate" recorded macro programming

dim wbkPs as Workbook, wbkNew as Workboo
dim shtEnvironment as Sheet, shtCT as Shee

Application.ScreenUpdating = False ' This will make it run faster, and your users won't see what's happening
set wbkPs = Workbooks("ps"
set shtEnvironment = wbkPs.Sheets("Environment"
set shtCT = wbkPs.Sheets("CT"

set wbkNew = Workbooks.Ad

shtEnvironment.Copy Befo=wbkNew.Sheets(1
shtCT.Copy Befo=wbkNew.Sheets(1
Application.ScreenUpdating = True ' Refresh the screen

I hope that helps

-Brad
  #3   Report Post  
Posted to microsoft.public.excel.programming
JER JER is offline
external usenet poster
 
Posts: 2
Default Naming convention for a variable "Book1" while copying sheets

Thanks Brad, but I ran into some problems ---

entered the following as a test in a workbook called ps
with a sheet named environment and another named CT.

They generated debug messages as indicated:

Sub tryanswer()
Application.ScreenUpdating = False
Dim wbkNew As Workbook
Set wbkNew = Workbooks.Add
'Error 9 "Subscript out of Range" points to next line
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=wbkNew.Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=wbkNew.Sheets(1)


'And consider this code to eliminate the whole "Select"
and "Activate" 'recorded macro programming.
End Sub

Sub tryanswer2()
Dim wbkPs As Workbook, wbkNew As Workbook
'Dim shtEnvironment As Sheet, shtCT As Sheet

Application.ScreenUpdating = False
' This will make it run faster, and your users won't see
what's happening.
'Error 9 "Subscript out of Range" points to next line
Set wbkPs = Workbooks("ps")
Set shtEnvironment = wbkPs.Sheets("Environment")
Set shtCT = wbkPs.Sheets("CT")

Set wbkNew = Workbooks.Add

shtEnvironment.Copy Befo=wbkNew.Sheets(1)
shtCT.Copy Befo=wbkNew.Sheets(1)
Application.ScreenUpdating = True
' Refresh the screen.
End Sub

-----Original Message-----
When you execute the Add method of the Workbooks

collection, set a variable to its return value. Then you
can keep the workbook object in memory, and refer to it
that way.

Original code:

Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=Workbooks

("Book1").Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=Workbooks("Book1").Sheets(1)

Recommended changes:

dim wbkNew as Workbook
set wbkNew = Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=wbkNew.Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=wbkNew.Sheets(1)

And consider this code to eliminate the whole "Select"

and "Activate" recorded macro programming.

dim wbkPs as Workbook, wbkNew as Workbook
dim shtEnvironment as Sheet, shtCT as Sheet

Application.ScreenUpdating = False ' This will make it

run faster, and your users won't see what's happening.
set wbkPs = Workbooks("ps")
set shtEnvironment = wbkPs.Sheets("Environment")
set shtCT = wbkPs.Sheets("CT")

set wbkNew = Workbooks.Add

shtEnvironment.Copy Befo=wbkNew.Sheets(1)
shtCT.Copy Befo=wbkNew.Sheets(1)
Application.ScreenUpdating = True ' Refresh the screen.

I hope that helps.

-Brad
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Naming convention for a variable "Book1" while copying sheets

I left out the line that you had in your original code..

ps = ThisWorkbook.Nam

The out of range errors are coming from the missing "ps" variable

-Brad
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Naming convention for a variable "Book1" while copying sheets

Set wbkPs = Workbooks("ps.xls")

Will always work.

Set wbkPs = Workbooks("ps")

will only work if certain Windows level options (not Excel) are chosen.

--
Regards,
Tom Ogilvy



"JER" wrote in message
...
Thanks Brad, but I ran into some problems ---

entered the following as a test in a workbook called ps
with a sheet named environment and another named CT.

They generated debug messages as indicated:

Sub tryanswer()
Application.ScreenUpdating = False
Dim wbkNew As Workbook
Set wbkNew = Workbooks.Add
'Error 9 "Subscript out of Range" points to next line
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=wbkNew.Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=wbkNew.Sheets(1)


'And consider this code to eliminate the whole "Select"
and "Activate" 'recorded macro programming.
End Sub

Sub tryanswer2()
Dim wbkPs As Workbook, wbkNew As Workbook
'Dim shtEnvironment As Sheet, shtCT As Sheet

Application.ScreenUpdating = False
' This will make it run faster, and your users won't see
what's happening.
'Error 9 "Subscript out of Range" points to next line
Set wbkPs = Workbooks("ps")
Set shtEnvironment = wbkPs.Sheets("Environment")
Set shtCT = wbkPs.Sheets("CT")

Set wbkNew = Workbooks.Add

shtEnvironment.Copy Befo=wbkNew.Sheets(1)
shtCT.Copy Befo=wbkNew.Sheets(1)
Application.ScreenUpdating = True
' Refresh the screen.
End Sub

-----Original Message-----
When you execute the Add method of the Workbooks

collection, set a variable to its return value. Then you
can keep the workbook object in memory, and refer to it
that way.

Original code:

Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=Workbooks

("Book1").Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=Workbooks("Book1").Sheets(1)

Recommended changes:

dim wbkNew as Workbook
set wbkNew = Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=wbkNew.Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=wbkNew.Sheets(1)

And consider this code to eliminate the whole "Select"

and "Activate" recorded macro programming.

dim wbkPs as Workbook, wbkNew as Workbook
dim shtEnvironment as Sheet, shtCT as Sheet

Application.ScreenUpdating = False ' This will make it

run faster, and your users won't see what's happening.
set wbkPs = Workbooks("ps")
set shtEnvironment = wbkPs.Sheets("Environment")
set shtCT = wbkPs.Sheets("CT")

set wbkNew = Workbooks.Add

shtEnvironment.Copy Befo=wbkNew.Sheets(1)
shtCT.Copy Befo=wbkNew.Sheets(1)
Application.ScreenUpdating = True ' Refresh the screen.

I hope that helps.

-Brad
.





  #6   Report Post  
Posted to microsoft.public.excel.programming
JER JER is offline
external usenet poster
 
Posts: 2
Default Naming convention for a variable "Book1" while copying sheets

Thankyou!

Have now got a handle on it!

JER
-----Original Message-----
Set wbkPs = Workbooks("ps.xls")

Will always work.

Set wbkPs = Workbooks("ps")

will only work if certain Windows level options (not

Excel) are chosen.

--
Regards,
Tom Ogilvy



"JER" wrote in

message
...
Thanks Brad, but I ran into some problems ---

entered the following as a test in a workbook called ps
with a sheet named environment and another named CT.

They generated debug messages as indicated:

Sub tryanswer()
Application.ScreenUpdating = False
Dim wbkNew As Workbook
Set wbkNew = Workbooks.Add
'Error 9 "Subscript out of Range" points to next

line
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=wbkNew.Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=wbkNew.Sheets(1)


'And consider this code to eliminate the whole "Select"
and "Activate" 'recorded macro programming.
End Sub

Sub tryanswer2()
Dim wbkPs As Workbook, wbkNew As Workbook
'Dim shtEnvironment As Sheet, shtCT As Sheet

Application.ScreenUpdating = False
' This will make it run faster, and your users won't see
what's happening.
'Error 9 "Subscript out of Range" points to next line
Set wbkPs = Workbooks("ps")
Set shtEnvironment = wbkPs.Sheets("Environment")
Set shtCT = wbkPs.Sheets("CT")

Set wbkNew = Workbooks.Add

shtEnvironment.Copy Befo=wbkNew.Sheets(1)
shtCT.Copy Befo=wbkNew.Sheets(1)
Application.ScreenUpdating = True
' Refresh the screen.
End Sub

-----Original Message-----
When you execute the Add method of the Workbooks

collection, set a variable to its return value. Then

you
can keep the workbook object in memory, and refer to it
that way.

Original code:

Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=Workbooks

("Book1").Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=Workbooks("Book1").Sheets

(1)

Recommended changes:

dim wbkNew as Workbook
set wbkNew = Workbooks.Add
Windows("ps").Activate
Sheets("Environment").Select
Sheets("Environment").Copy Befo=wbkNew.Sheets(1)
Windows("ps").Activate
Sheets("CT").Select
Sheets("CT").Copy Befo=wbkNew.Sheets(1)

And consider this code to eliminate the whole "Select"

and "Activate" recorded macro programming.

dim wbkPs as Workbook, wbkNew as Workbook
dim shtEnvironment as Sheet, shtCT as Sheet

Application.ScreenUpdating = False ' This will make it

run faster, and your users won't see what's happening.
set wbkPs = Workbooks("ps")
set shtEnvironment = wbkPs.Sheets("Environment")
set shtCT = wbkPs.Sheets("CT")

set wbkNew = Workbooks.Add

shtEnvironment.Copy Befo=wbkNew.Sheets(1)
shtCT.Copy Befo=wbkNew.Sheets(1)
Application.ScreenUpdating = True ' Refresh the screen.

I hope that helps.

-Brad
.



.

  #7   Report Post  
Posted to microsoft.public.excel.programming
JER JER is offline
external usenet poster
 
Posts: 2
Default Naming convention for a variable "Book1" while copying sheets

Thanks Brad.

Between you and Tom Olgivy I am now on the functioning
track to what I need done!

JER

-----Original Message-----
I left out the line that you had in your original code...

ps = ThisWorkbook.Name

The out of range errors are coming from the missing "ps"

variable.

-Brad
.

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
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Stop "Book1" from opening from a saved file jcutolo Excel Discussion (Misc queries) 4 July 27th 07 08:08 PM
"Type mismatch" when I try to fill an Array variable with "+" [email protected] Excel Discussion (Misc queries) 1 April 17th 07 01:28 PM
Naming cell ranges, copying formulas for a range & nesting "IF" fu DonF Excel Discussion (Misc queries) 3 October 5th 06 05:47 PM
Why is "History" a "reserved name" while naming Excel worksheets? Pradeep Excel Discussion (Misc queries) 1 June 30th 06 12:55 PM


All times are GMT +1. The time now is 07:26 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"