|
-
Jul 24th, 2008, 05:30 AM
#1
Thread Starter
Junior Member
[RESOLVED] How to display ToolBar by Worksheet name
Hello members,
This problem is realy annoying me : I have a VB macro which is triggered by a toolbar icon which created by the macro , the problem here is that i need this icon to be shown (visible) only if the opened excel included work sheet with a specific name suppose "MyMacro" .the problem is that i cant get the name of the opened workbook till i press the icon .
I'll appreciate any solution for that problem.
Thanks In advance
Jozef B.
-
Jul 24th, 2008, 06:03 AM
#2
Re: How to display ToolBar by Worksheet name
Moved To Office Development
-
Jul 24th, 2008, 06:39 AM
#3
Re: How to display ToolBar by Worksheet name
load the tool bar icon on workbook open event
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 4th, 2008, 06:49 AM
#4
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Thanks Wetconn1 ..., and sorry for the late response(just returned from a vacation) , this sound like a great solution i'll try it and update you back
-
Aug 4th, 2008, 08:53 AM
#5
Thread Starter
Junior Member
Still Looking for solution
Hell There
I've tried this solution to create toolbar icon from workbook_open event but it didn't worked out, since when this event is triggered the system still doesn't have the active workbook name because it didn't fully opened yet....
Sincerly Jozef..
-
Aug 4th, 2008, 04:34 PM
#6
Re: How to display ToolBar by Worksheet name
i just tested this,
vb Code:
Private Sub Workbook_Open() For i = 1 To Sheets.Count MsgBox Sheets(i).Name Next End Sub
worked as expected, so what you want should work, do you get an error or what?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 5th, 2008, 12:20 AM
#7
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Thanks Again I'll try this solution and response to you with the results, I appreciate it..
-
Aug 6th, 2008, 12:38 AM
#8
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Hello westconn1 and thanks again for your efforts to solve this problem ,
let me explain the problem in more details :
I have a macro : "Test.xla"
this macro has 3 sheets "Test1","Test2","Test3" which is not important but created during saving the macro file , there is also part of the macro functions(modules) which is responsible for creating the icon i had mentioned above ,and of course executing the icon functions as required.
when i open an excel file and asks about "sheets(1-3).name" as you described above i get sheets "Test1-Test3" of the current xla file but the whole problem is to know the sheet name of the opened excel file or any other excel file and to decide by its sheets name or workbook's name if to display or not display the icon , but I can't get the name of the opened excel sheets only the xla sheets which is not important.
Thanks again , I hope this issue is much clear now , if not i will be happy to eleborate much more ....
Sincerely Jozef
-
Aug 6th, 2008, 02:55 AM
#9
Re: How to display ToolBar by Worksheet name
ok, i thought the code would be in the workbook that contained the sheets, try specifying the workbook you want the sheets of
vb Code:
for i = 1 to workbooks("theworkbookiwant.xls").sheets.count msgbox workbooks("theworkbookiwant.xls").sheets(i).name next
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 6th, 2008, 07:43 AM
#10
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
That exactly the problem , that I can't put the code in the desired workbook since I don't know which workbook the user will open , the code is located inside my macro at workbook_open event of an xla file "Test.XLA" and its suppose to show the icon depends on the workbook name that the user is opening . when user open an excel file then my xla macro file (Add-In) is loaded first and at this point it cant know what the user workbook name .
sorry for not making my self clear but i am trying as hard as possible and i really appreciate all your responses , I guess finally we will found solution..
-
Aug 6th, 2008, 04:22 PM
#11
Re: How to display ToolBar by Worksheet name
try moving the code to the workbook activate event
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 7th, 2008, 12:56 AM
#12
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
OK , Thats sound interesting...
Thnks , Jozef
-
Aug 7th, 2008, 05:05 AM
#13
Re: How to display ToolBar by Worksheet name
on thinking more about this, when the user opens an new workbook the current workbook (i think your macro file) would be deactivated, maybe code could go in the deactivate event for your macro file workbook, to loop through open workbooks to select the sheets as required
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 9th, 2008, 02:23 AM
#14
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Hello Westconn1
---------------
Sorry for the late reply , but i am not available to the email acount only at work time, I couldn't do it with work_book activate event , but your last reply seems very close to a solution , I need to deactivate the current work book and to hope that the loop will find the last active work book which the user opened lastly- and is last at the workbooks openning sequel
I will try it , but thats sounds like a brilliant move..
Sincerely Jozef
-
Aug 10th, 2008, 05:56 AM
#15
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Hello again westconn1 - here is my finding:I've tested the code on workbook_deactivate event and nothing happened when i asked for workbooks names , as matter of fact i tested different workbooks events and the event workbook_beforeclose could control other workbooks and give all the names but the timing of the event is when i close the workbook and its to late.
-
Aug 10th, 2008, 07:51 AM
#16
Re: How to display ToolBar by Worksheet name
can you post a sample workbook that you are working with?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 11th, 2008, 01:33 AM
#17
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Sorry Westconn1 , I am new to this forum i couldn't find how to attach files , but i'll to write here the relevant part from the macro code so there no need for all the code :
at the macro file suppose its name "test.xla" i wrote the following code under "This WorkBook" module :
Private Sub Workbook_Open()
For i = 1 To workbooks.Count
MsgBox workbook(i).name
End Sub
now suppose i am openning an excel file : "Employee.xls"
the macro return the name "Personal.xls" and not "Employee.xls"
The openning order is : 1) "Test.xla" , 2) "Personals.xls" , 3) " "Employee.xls"
but i need the "Employee.xls" and the macro can't return this name .
I found something very interesting : If i put the code under "Personals.xls" and not in my macro file "Test.xla" then when I run the procedure i receive all the work books name ??? - this a by pass solution but i cant use it since i don't want my macro to run from personals, if we could change the openning sequel it might be solved.
Thanks for your efforts , and patience i really appreciate it.
Sincerely Jozef
-
Aug 11th, 2008, 01:35 AM
#18
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Sorry for miss typing - of course there is NEXT I at the end of FOR
-
Aug 11th, 2008, 02:31 AM
#19
Re: How to display ToolBar by Worksheet name
to attach files you have to "Go Advanced" > manage attachments
this works for me in a std workbook, when i open another workbook it runs
vb Code:
Private Sub Workbook_Deactivate() For Each w In Workbooks If Not w.Name = ThisWorkbook.Name Then For Each s In w.Sheets MsgBox s.Name Next End If Next End Sub
edit: as an .xla is not visible on screen it is quite likely it has no deactivate event, possibly the only way to do what you want is to use some form of timer to check if an additional work book is open
Last edited by westconn1; Aug 11th, 2008 at 02:49 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 11th, 2008, 06:26 AM
#20
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Hello Westconn1,I put your code above in the personals.xls under "ThisWorkbook" module file and it works fine and find all the requested sheets name , but as you mentioned there is problem with xla fie (its workbook is shown in project explorer list but can not use deactivate event) . you also mentioned something about using the timer function , could you please tell me much more details about it : like where in the code/modules to put it and how much delay to set....
Thanks for the tip about attaching files but I guess we will not need it since now we knows were the problem is and we just look for proper solution..
P.S: I am just wondering if I am the only one who encountered this problem or other people too ...
Finally Thanks again your are being very helpfull
-
Aug 11th, 2008, 06:28 AM
#21
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Hello Westconn1,
I jut put your code above in the personals.xls under "ThisWorkbook" module and it works fine and find all the requested sheets name , but as you mentioned there is problem with xla fie (its workbook is shown in project explorer list but can not use deactivate event) . you also mentioned something about using the timer function , could you please tell me much more details about it : like where in the code/modules to put it and how much delay to set....
Thanks for the tip about attaching files but I guess we will not need it since now we knows were the problem is and we just look for a proper solution..
P.S: I am just wondering if I am the only one who encountered this problem or other people too ,since it seems like a very basic request.
Finally Thanks again - your are being very helpfull
-
Aug 11th, 2008, 07:14 AM
#22
Re: How to display ToolBar by Worksheet name
there is no timer control available to use in vba, so you would have to either use API timers or application.ontime to work as a timer
another possible option is to use your addin to replace the fileopen code on the menu to provide the functionality you want, whenever someone clicks fileopen while your addin is running
see if any of the example here can help, i am sure better can be found, on doing more searching http://www.cpearson.com/excel/CreateAddIn.aspx
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 11th, 2008, 04:20 PM
#23
Re: How to display ToolBar by Worksheet name
ok if you add a class module to your .xla and add application events, you can then get the event of any workbook in the appliaction
i tested this in a .xla
class module class1
vb Code:
Public WithEvents App As Application Private Sub App_WorkbookActivate(ByVal Wb As Workbook) MsgBox Wb.Name End Sub
in thisworkbook code module for the .xla
vb Code:
Dim X As New Class1 Sub InitializeApp() Set X.App = Application End Sub
you can move the code set x.app = application into workbook open event of the .xla for it to run automatically
there is also a workbook open event that may be better for your purpose
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 12th, 2008, 01:44 AM
#24
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Thanks A Lot Westconn1 , I need time to process all this precious info , since its new to me and I am studying a lot here.
thats why I am delaying with the results , but I am happy i have this opportunity to study new issues in VB .
-
Aug 12th, 2008, 09:24 AM
#25
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Thanks Again Westconn1 for the information and let me share you with my solution, thanks to you.
Here is what I did: I put the following code under class1 module on xla file:
1. Private Sub App_WorkbookDeactivate(ByVal Wb As Workbook)
2. Dim ShName,Max
3. Max = Workbooks.Count
4. ShName = (Workbooks(Max).Worksheets(1).Name)
5. If ShName = "MySheet" Then
6. ToolBarAdd
7. Else
ToolBarHide
8. End If
9. End Sub
And its doing the Job !!
now I get the relevant workbook and workheet name since he is max and i can trigger it now due to application events, but I have to write all the functions under Class1 module and I was just wondering if I can use the x variable which I defined as an instance of ApplicationEvents but is located at "workkbook_open" event as you told me ,when I've tried to use it to ask for x.workbook name then the whole problem starts again and I get the "personals.xls" workbook but anyway I am very thankfull
for your patience and efforts and feel very lucky to have your assistance which led me to the solution, finally.
Sincerely Jozef
-
Aug 12th, 2008, 04:27 PM
#26
Re: How to display ToolBar by Worksheet name
app also has a sheet activate event, as you want the tooolbar to show based on sheet name you could use this event
vb Code:
Private Sub App_SheetActivate(ByVal Sh As Object) if sh.name = "somesheet" then 'toolbar visible code else ' toolbar not visible code end if End Sub
you also need to consider if other workbooks are opened then the workbook you are activating may no longer be max index
Last edited by westconn1; Aug 12th, 2008 at 04:31 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 13th, 2008, 03:49 AM
#27
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Sure, but now the customer asks to identify the icon by workbookname so I put the code on App_WorkbookActivate without using Max and its working fine 
I feel very lucky to find this forum and this specific thread and its experts....
I am new to vb programming and i am amazed by its power and functionallity ,
and I am very eager to learn more new issues(Will be very happy for any updates on VB issues ) , and till then
Have a nice Day and Thanks for the guide
Sincerely Jozef
-
Aug 13th, 2008, 04:05 AM
#28
Re: How to display ToolBar by Worksheet name
if it is all working now, please mark thread resolved
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 13th, 2008, 11:50 AM
#29
Thread Starter
Junior Member
Re: How to display ToolBar by Worksheet name
Last edited by yossibnv; Aug 13th, 2008 at 03:42 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|