PC Review


Reply
Thread Tools Rate Thread

Determining which workbook(index) is the one closing

 
 
Clark Kent
Guest
Posts: n/a
 
      11th Apr 2008
Sorry, if this sounds like dumb question but I'm an excel programming noob.

Basically what I'm trying to do is determine what workbook(index wise) is
the one closing during the "Worbook before Close" method.

For example, lets say I have 5 workbooks open in my Excel session and I
close one of them. I could access them easily by saying
Application.Workbooks[1], etc. However, I just want to know if there is a way
to know which one I'm currently closing.

I can easily count my number of open workbooks using workbook.count but I am
not able to go from that to the actual index of the one I'm closing...

I'm doing this in C# if it helps but I can probably translate VB if someone
knows how to do it...
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      11th Apr 2008
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox ("Workbook: " & ActiveWorkbook.Name & " is closing")
i = 1
For Each w In Workbooks
If w.Name = ActiveWorkbook.Name Then
MsgBox ("by the way, i am workbook # : " & i)

End If
i = i + 1
Next
End Sub

--
Gary''s Student - gsnu200778


"Clark Kent" wrote:

> Sorry, if this sounds like dumb question but I'm an excel programming noob.
>
> Basically what I'm trying to do is determine what workbook(index wise) is
> the one closing during the "Worbook before Close" method.
>
> For example, lets say I have 5 workbooks open in my Excel session and I
> close one of them. I could access them easily by saying
> Application.Workbooks[1], etc. However, I just want to know if there is a way
> to know which one I'm currently closing.
>
> I can easily count my number of open workbooks using workbook.count but I am
> not able to go from that to the actual index of the one I'm closing...
>
> I'm doing this in C# if it helps but I can probably translate VB if someone
> knows how to do it...

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      11th Apr 2008
Or, as an alternate, this way...

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim X As Long
MsgBox "Workbook: " & ActiveWorkbook.Name & " is closing"
For X = 1 To Workbooks.Count
If Workbooks(X).Name = ActiveWorkbook.Name Then
MsgBox "By the way, I am workbook #" & X
End If
Next
End Sub


Rick


"Gary''s Student" <(E-Mail Removed)> wrote in message
news:99676D47-E510-4A15-AAF0-(E-Mail Removed)...
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> MsgBox ("Workbook: " & ActiveWorkbook.Name & " is closing")
> i = 1
> For Each w In Workbooks
> If w.Name = ActiveWorkbook.Name Then
> MsgBox ("by the way, i am workbook # : " & i)
>
> End If
> i = i + 1
> Next
> End Sub
>
> --
> Gary''s Student - gsnu200778
>
>
> "Clark Kent" wrote:
>
>> Sorry, if this sounds like dumb question but I'm an excel programming
>> noob.
>>
>> Basically what I'm trying to do is determine what workbook(index wise) is
>> the one closing during the "Worbook before Close" method.
>>
>> For example, lets say I have 5 workbooks open in my Excel session and I
>> close one of them. I could access them easily by saying
>> Application.Workbooks[1], etc. However, I just want to know if there is a
>> way
>> to know which one I'm currently closing.
>>
>> I can easily count my number of open workbooks using workbook.count but I
>> am
>> not able to go from that to the actual index of the one I'm closing...
>>
>> I'm doing this in C# if it helps but I can probably translate VB if
>> someone
>> knows how to do it...


 
Reply With Quote
 
Clark Kent
Guest
Posts: n/a
 
      11th Apr 2008
Beautiful, thanks! I didnt realize there was an "Active Workbook" object.
Should have known... :-)

"Gary''s Student" wrote:

> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> MsgBox ("Workbook: " & ActiveWorkbook.Name & " is closing")
> i = 1
> For Each w In Workbooks
> If w.Name = ActiveWorkbook.Name Then
> MsgBox ("by the way, i am workbook # : " & i)
>
> End If
> i = i + 1
> Next
> End Sub
>
> --
> Gary''s Student - gsnu200778
>
>
> "Clark Kent" wrote:
>
> > Sorry, if this sounds like dumb question but I'm an excel programming noob.
> >
> > Basically what I'm trying to do is determine what workbook(index wise) is
> > the one closing during the "Worbook before Close" method.
> >
> > For example, lets say I have 5 workbooks open in my Excel session and I
> > close one of them. I could access them easily by saying
> > Application.Workbooks[1], etc. However, I just want to know if there is a way
> > to know which one I'm currently closing.
> >
> > I can easily count my number of open workbooks using workbook.count but I am
> > not able to go from that to the actual index of the one I'm closing...
> >
> > I'm doing this in C# if it helps but I can probably translate VB if someone
> > knows how to do it...

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      11th Apr 2008
But it might not necessarily be the ActiveWorkbook. The event as written
will only fire in the ThisWorkbook module of a workbook, so in the event
could -

Msgbox Me.Name

then loop workbooks to return the index until Me.Name is found (whatever for
though ?)

Regards,
Peter T


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:%23YMzoz$(E-Mail Removed)...
> Or, as an alternate, this way...
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> Dim X As Long
> MsgBox "Workbook: " & ActiveWorkbook.Name & " is closing"
> For X = 1 To Workbooks.Count
> If Workbooks(X).Name = ActiveWorkbook.Name Then
> MsgBox "By the way, I am workbook #" & X
> End If
> Next
> End Sub
>
>
> Rick
>
>
> "Gary''s Student" <(E-Mail Removed)> wrote in
> message news:99676D47-E510-4A15-AAF0-(E-Mail Removed)...
>> Private Sub Workbook_BeforeClose(Cancel As Boolean)
>> MsgBox ("Workbook: " & ActiveWorkbook.Name & " is closing")
>> i = 1
>> For Each w In Workbooks
>> If w.Name = ActiveWorkbook.Name Then
>> MsgBox ("by the way, i am workbook # : " & i)
>>
>> End If
>> i = i + 1
>> Next
>> End Sub
>>
>> --
>> Gary''s Student - gsnu200778
>>
>>
>> "Clark Kent" wrote:
>>
>>> Sorry, if this sounds like dumb question but I'm an excel programming
>>> noob.
>>>
>>> Basically what I'm trying to do is determine what workbook(index wise)
>>> is
>>> the one closing during the "Worbook before Close" method.
>>>
>>> For example, lets say I have 5 workbooks open in my Excel session and I
>>> close one of them. I could access them easily by saying
>>> Application.Workbooks[1], etc. However, I just want to know if there is
>>> a way
>>> to know which one I'm currently closing.
>>>
>>> I can easily count my number of open workbooks using workbook.count but
>>> I am
>>> not able to go from that to the actual index of the one I'm closing...
>>>
>>> I'm doing this in C# if it helps but I can probably translate VB if
>>> someone
>>> knows how to do it...

>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use index and match function for determining the value? Eric Microsoft Excel Misc 1 7th Aug 2009 06:09 AM
Determining whether a workbook is a 2003 or 2007 workbook sleepp Microsoft Excel Programming 6 4th Feb 2009 03:13 PM
Access to PPoint / Determining SHAPE (INDEX) w/VBA Rob Microsoft Powerpoint 6 8th Jul 2007 08:05 PM
determining the index of a row in a datatable JohnR Microsoft VB .NET 2 4th Nov 2005 03:33 AM
Determining the Parent orm is closing Sumit Microsoft VB .NET 3 21st Sep 2004 02:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:01 AM.