View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Shorten code to apply to all sheets except a few, instead of individually naming them, and later adding to code.

It is pretty hard to figure out what you are doing with all the selects and
sheet activates, but this is my guess for the Loop part:

for the part past the loop, you are calling procedures I can't see and it
depends on which sheet is active and so forth - and I have no idea which
sheet is active - and it is always best to avoid writing code that depends
on a sheet being active.


in my code,
bk1 is the workbook that was added. (Temporary Name.xls I would Assume)

bk is the workbook active at the time you started the procedure.
(Timesheets.xls I would assume)

Sub Export_TimeSheets()
Dim bk as Workbook, bk1 as Workbook
set bk = ActiveWorkbook
Call Unprotect
res = InputBox("Are you sure you want to Store the TimeSheets NOW ?" &
vbCrLf & vbCrLf & "This will not allow for anymore modifications...." &
vbCrLf & vbCrLf & vbTab & vbTab & "yes - (All lower case to continue).",
"....")
If res < "yes" Then
MsgBox "Please change what you need to and then try again.", , "...."
exit sub
end if
Application.ScreenUpdating = False
Application.DisplayAlerts = False
' Create a New WorkBook and Copy TimeSheets into it for Storage
' name 8 TimeSheet
Workbooks.Add
set bk1 = ActiveWorkbook
' Temporarily Name & Save the WorkBook into the TimeSheets Folder....
bk1.SaveAs Filename:= _
"\\Office2\my documents\TimeSheets\Temporary Name.xls", _
FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
Application.DisplayAlerts = False

'
' loop to copy sheets
'
for each sh in bk.Worksheets
if sh.Name < "Sheet1" and sh.Name < _
"sheet2" and sh.Name < "Sheet3" then
set sh1 = bk1.Worksheets.Add(
After:=bk1.Worksheets(bk1.Worksheets.count))
sh.Range("a1:u41").Copy
Application.CommandBars("Task Pane").Visible = False
sh1.Range("A:A,D:D,G:G,J:J,M:M,P:P,S:S").ColumnWid th = 1
sh1.Range("A1:U42").PasteSpecial Paste:=xlPasteValues
sh1.Range("A1:U42").PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
' Place a Border Around TimeSheet
With sh1.Range("A1:U42")
.Item(1).Activate
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
with .Borders(xlEdgeLeft)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = 56
End With
With .Borders(xlEdgeTop)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = 56
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = 56
End With
With .Borders(xlEdgeRight)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = 56
End With
Range("A1").Select

sh1.Name = sh1.Range("K2").Value
sh1.Cells.Select
sh1.protect DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True
sh1.EnableSelection = xlNoSelection
Application.DisplayAlerts = False


Next sh
'
' End of loop to copy sheets
'


' no way I can tell what is going on in the next sequence. You call
' procedures I can not see and take actions on the activesheet, but no way I
can
' know what sheet is active since you have executed unknown code with your
calls to
' SheetMove and SheetMove2 and so forth.



' Copy/Paste Utilization Sheet with graphs
Call SheetMove
Columns("A:B").Select
Selection.ColumnWidth = 10
Columns("C:AA").Select
Selection.ColumnWidth = 5
Columns("X:X").Select
Selection.ColumnWidth = 7
ActiveSheet.Name = Worksheets("Utilization Sheet").Range("B1").Value
Range("A1").Select
Call SheetMove2
With Sheets("Sheet2").Delete
End With
With Sheets("Utilization Sheet")
.Select
ActiveSheet.Shapes("CommandButton22").Select
Selection.Delete
End With
Sheets("Leave Blank").Select
ActiveWindow.DisplayWorkbookTabs = True
' Name & Save the WorkBook into the TimeSheets Folder
ChDir "\\Office2\my documents\TimeSheets"
ActiveWorkbook.SaveAs Filename:= _
"\\Office2\my documents\TimeSheets\" & Sheet1.Range("E2").Text &
".xls", FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=True, _
CreateBackup:=False
Call protect
ActiveWorkbook.Close
Sheets("Enter - Exit").Select
Dim myrange As Range
Set myrange = Range("E2")
Application.DisplayAlerts = True ' Turning it back on
MsgBox "The TimeSheets have been Stored as the Week Ending " &
myrange.Value & " .", , "...."
Workbooks("TimeSheets").Activate
Sheets("Enter - Exit").Select
Range("A1").Select
Call Unprotect
Application.ScreenUpdating = False
Sheets("name 8").Select
Call ClearTimeSheetValues
Sheets("Enter - Exit").Select
Range("A1").Select
Call protect
Application.DisplayAlerts = False
If Dir("\\Office2\my documents\TimeSheets\Temporary Name.xls") < ""
Then _
Kill "\\Office2\my documents\TimeSheets\Temporary name.xls"

Application.DisplayAlerts = True ' Turning it back on

End sub

--
regards,
Tom Ogilvy