|
-
May 4th, 2006, 05:21 PM
#1
Thread Starter
New Member
create CommandButtons
Hi,
I've been trying to create some buttons on a single tab or each button for every tab that I have in a workbook, with the VBA command in VBE codemodule. The button part is fine but evey time as try to write the code for each button (not manually but using VBA), Excel session will be crashed. The code I use goes like this:
Sub AddSomeButtons()
'On Error Resume Next
Application.ScreenUpdating = False
Dim NewButton As OLEObject
For Each c In ThisWorkbook.VBProject.VBComponents
Set WBCodeMod = c.CodeModule
Next
ThisWorkbook.VBProject.VBComponents.CodeModule _
.DeleteLines 1, LineCount
For x = 1 To Worksheets.Count
If Sheets(x).Name <> "contents" Then
Set NewButton = ActiveSheet.OLEObjects.Add _
("Forms.CommandButton.1")
With NewButton
.Left = 4
.Top = 40 * x
.Width = 54
.Height = 20
.Object.Caption = Sheets(x).Name
End With
ThisWorkbook.VBProject.VBComponents.CodeModule _
.AddFromString _
"Private Sub CommandButton" & x & "_Click" & vbCr & _
"Msgbox ""This is CommandButton" & x & """" & vbCr & _
"End Sub"
End If
Next
Application.ScreenUpdating = True
On Error GoTo 0
End Sub
The statement between the frogs is always the trouble-spot. If I don't use the For loop, i.e., create a single button with the code, then fine. Or it crashes.
Thanks in advance for sharing some input if any.
Feipe
-
May 4th, 2006, 06:06 PM
#2
Re: create CommandButtons
-
May 4th, 2006, 06:15 PM
#3
Re: create CommandButtons
My CodeBank thread should help you to interact with dynamic coding of vba code.
http://www.vbforums.com/showthread.php?t=313861
But looks like your missing the last "()" on your event procedure line.
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 
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
|