Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell | Excel Discussion (Misc queries) | |||
Stop "Book1" from opening from a saved file | Excel Discussion (Misc queries) | |||
"Type mismatch" when I try to fill an Array variable with "+" | Excel Discussion (Misc queries) | |||
Naming cell ranges, copying formulas for a range & nesting "IF" fu | Excel Discussion (Misc queries) | |||
Why is "History" a "reserved name" while naming Excel worksheets? | Excel Discussion (Misc queries) |