View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tim[_44_] Tim[_44_] is offline
external usenet poster
 
Posts: 28
Default Workbook.Activate / Window.Activate problem

OK. I give up. Someone smarter than me is going to need to tell me
why I am deleting the worksheet from the wrong workbook. Seems like I
can't figure out how to correctly shift from one to another. The last
statement here gives me a 'subscript out of range' error, and I can't
figure out why.

Code follows:

Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal
Target As Range)
Dim cell As Range
Dim wrksht As Worksheet
Dim SendMail As Outlook.MailItem
Dim i%, ReportPage$, ReportRow%, Subject$, SelectedAddresses$,
ScheduleName$, IndividualName$

If sh.Name Like "####Q#" Then
If Target.Row <= 2 And Cells(Target.Row, Target.Column).Value <
"" Then
If MsgBox("Generate report for " & Range(sh.Name & "!" &
Cells(1, Target.Column).Address).Value & " " & Range(sh.Name & "!" &
Cells(2, Target.Column).Address).Value & "?", vbYesNo) = vbYes Then
ScheduleName$ = Left(ActiveWorkbook.Name,
Len(ActiveWorkbook.Name) - 4)
IndividualName$ = ScheduleName$ & " " & Range(sh.Name & "!"
& Cells(1, Target.Column).Address).Value & " " & Range(sh.Name & "!" &
Cells(2, Target.Column).Address).Value
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=IndividualName$
Workbooks(ScheduleName$ & ".xls").Activate
Sheets(Array(Left(ActiveSheet.Name, 5) & "1",
Left(ActiveSheet.Name, 5) & "2", Left(ActiveSheet.Name, 5) & "3",
Left(ActiveSheet.Name, 5) & "4")).Copy
befo=Workbooks(IndividualName$ & ".xls").Sheets(1)
Workbooks(IndividualName$ & ".xls").Activate
' Windows("CPF Time Off Schedule V2 Mia
Bijaksana.xls").Activate
'Sheets("Sheet1").Delete
Workbooks(ScheduleName$ & ".xls").Activate
Windows(ScheduleName$ & ".xls").Activate
Workbooks(IndividualName$ & ".xls").Activate
Windows(IndividualName$ & ".xls").Activate