|
-
Nov 14th, 2005, 05:19 AM
#1
Thread Starter
New Member
A Challenge - Adding Buttons to an Excel Worksheet
Hi Everyone,
I've got a challenge I've been trying to work through today, and any ideas would be welcome. I have the following code:
1) Create a list of all the sheets in a workbook
Sub ListSheets()
Dim Mysheet As Object
ActiveSheet.Range("A1").Activate
For Each Mysheet In Sheets
ActiveCell.Value = Mysheet.Name
ActiveCell.Offset(1, 0).Activate
Next Mysheet
End Sub
2) Create buttons and code to jump to all the worksheets in the book, on the active sheet
Sub AddSheetAndButtons2()
Dim Code As String, Count As Integer
Dim NewButton As OLEObject
Dim NewSheet As Worksheet
Dim Mysheet As Object, LinkToSheet As String, NextLine As Integer
ActiveSheet.Range("A1").Activate
Do Until ActiveCell.Value = ""
Count = Count + 1
LinkToSheet = ActiveCell.Value
'Add a new button
Set NewButton = ActiveSheet.OLEObjects.Add("Forms.CommandButton.1")
NewButton.Name = "CB" & Count
With NewButton
.Left = 10
.Top = 28 * Count
.Width = 150
.Height = 25
.Object.Caption = LinkToSheet
End With
'Add Code
Code = ""
Code = "Private Sub CommandButton" & Count & "_Click()" & vbCrLf
Code = Code & vbTab & "ActiveWorkbook.Sheets(" & Chr(34) & LinkToSheet & Chr(34) & ").Activate" & vbCrLf
Code = Code & "End Sub"
'Insert code into module
With ActiveWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
NextLine = .CountOfLines + 4
.InsertLines NextLine, Code
End With
ActiveCell.Offset(1, 0).Activate
Loop
End Sub
Unfortunately after the workbook creates the first button succesfully. I get a subscript out of range error when I identify the VBComponents index.
I'm hoping someone out there can suggest where I'm going wrong.
Thanks for your help
Lothdyn
EDIT:
I just realised, a version of Excel would likely help. I'm trying to get this working in Excel 2000 (or failing that 2004).
Last edited by Lothdyn; Nov 14th, 2005 at 05:22 AM.
Reason: Some useful points missing
-
Nov 14th, 2005, 07:10 AM
#2
Re: A Challenge - Adding Buttons to an Excel Worksheet
Moved to Office Development
-
Nov 14th, 2005, 02:10 PM
#3
Re: A Challenge - Adding Buttons to an Excel Worksheet
Probably since you are using ActiveSheet which may or may not be the sheet that has a button on it. You should reference the sheets either directly by name or by setting an object variable to each sheet needed to be modified. Then there will be no doubt which sheet your working with.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 15th, 2005, 02:41 AM
#4
Thread Starter
New Member
Re: A Challenge - Adding Buttons to an Excel Worksheet
Thanks for the responses.
Unfortunately moving to office development is not an option as I'm a business analyst, not a programmer (hence the use of VBA).
I've had trouble when setting the sheet. For example, if I'm trying to set up sheet1 I use the commands (say
set newsheet = sheet1
With ActiveWorkbook.VBProject.VBComponents(newsheet.Name).CodeModule
NextLine = .CountOfLines + 4
.InsertLines NextLine, Code
End With
When using this code, I still end up the error. I'll give it another go a bit later.
Lothdyn
-
Nov 15th, 2005, 09:18 AM
#5
Frenzied Member
Re: A Challenge - Adding Buttons to an Excel Worksheet
Lothdyn ...
What is the maximum number of sheets you will be dealing with? One thing you can do to make this much easier is to design in the buttons into the original workbook and install the code, but disable them and set them not visible. Then, when you run the "ListSheets" code, you can just enable the buttons and make them visible. That is easy ... you don't have to dynamically load the code for the event handlers.
I have never played with controls before, and tried out your interesting routines. The error I get (Excel 2003) is:
Run-time error '1004':
Programmatic access to Visual Basic Project is not trusted
I have no idea what this means or how to get around it except to "design in" the controls and code from the beginning. If you decide to try this approach and have problems, I'm sure we can help achieve that.
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Nov 15th, 2005, 10:12 AM
#6
Re: A Challenge - Adding Buttons to an Excel Worksheet
Since your trying to access the vbproject of the workbook you need to manually check the "Trust access to your Visual Basic Project" in the Tools > Macro > Security dialog.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 1st, 2007, 04:21 AM
#7
Frenzied Member
Re: A Challenge - Adding Buttons to an Excel Worksheet
hi was wondering if you can help
ive a excel sheet which emails from VBA inside
however because i use Access 2003 it comes up with a box to say running VBA do i wish to send, ive looked at the tab for trust VBA however it is greyed out
basically want to auto email and not have to press yes everytime is there a way around this, cant tick the trust VBA as its greyed out, is there a way or changing the regitry so it isnt or add to stop the message appear
thanks in advance
could you email a reply to [email protected]
ok thanks
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Nov 1st, 2007, 04:37 AM
#8
Re: A Challenge - Adding Buttons to an Excel Worksheet
robdog has some code to work around this issue, check his signature or tutorial, also take out your email address
also a new thread with appropriate title would have been better
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
-
Nov 1st, 2007, 05:07 AM
#9
Frenzied Member
Re: A Challenge - Adding Buttons to an Excel Worksheet
rite ok
http://www.vbforums.com/showthread.php?t=402086
have found this, but it doesnt tell me how to get round it?? could u put the link in where it does?
i know what the problem is just cant get round it??
i havent got the trust VBA as its greyed out! its fig 3 on that link by the way, has a slight delay
havent a clue how to setup a trust thing, as its from a excel spreadsheet(VBA)
please help thanks
p.s what is the default VBA instance mean??
heres my code
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = EmailAddr
.CC = ""
.BCC = ""
.Subject = Subj
.Body = BodyText
'.Attachments.Add ActiveWorkbook.FullName
'.Display 'or use
.send
End With
Last edited by Robbo; Nov 1st, 2007 at 05:11 AM.
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
-
Nov 1st, 2007, 10:21 AM
#10
Frenzied Member
Re: A Challenge - Adding Buttons to an Excel Worksheet
hi added the trusted cerificate publisher however it still comes up with the box
please help
-----------------------------------------------
"The hall is rented,"
"the orchestra is engaged,"
"its now time to see if you can dance!"
Q, Q-Who, Star Trek The Next Generation
-----------------------------------------------
General Work day

-----------------------------------------------
DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle
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
|