-
Apr 13th, 2019, 09:34 AM
#1
Thread Starter
Member
Can you add a macro to Excel worksheet button through VB.NET?
I already created a commandbutton1 in sheet1 through vb.net. Also I already added a macro/vba code but in Module only.My only problem is on how to insert the macro/vba code inside the commandbutton1 click event.
-
Apr 14th, 2019, 09:21 AM
#2
Addicted Member
Re: Can you add a macro to Excel worksheet button through VB.NET?
You can create a code module with an event procedure for your button and then insert your lines of code. The create event proc returns a value which is the first line of the code module.The following is from an online tutorial. If you temporarily comment the last line of this code the VBA editor will remain open and you will be able to view the results of inserting the lines of code.
Code:
Dim LineOne As Long
With myWorkBook.VBProject.VBComponents.Item(myWorkSheet.CodeName).CodeModule
LineOne = .CreateEventProc("Click", "commandbutton1") + 1
.InsertLines(LineOne, "Sheets.Add After:=Sheets(Sheets.Count)")
End With
myWorkBook.VBE.MainWindow.Visible = False
-
Apr 14th, 2019, 09:50 PM
#3
Thread Starter
Member
Re: Can you add a macro to Excel worksheet button through VB.NET?
Originally Posted by Mc_VB
You can create a code module with an event procedure for your button and then insert your lines of code. The create event proc returns a value which is the first line of the code module.The following is from an online tutorial. If you temporarily comment the last line of this code the VBA editor will remain open and you will be able to view the results of inserting the lines of code.
Code:
Dim LineOne As Long
With myWorkBook.VBProject.VBComponents.Item(myWorkSheet.CodeName).CodeModule
LineOne = .CreateEventProc("Click", "commandbutton1") + 1
.InsertLines(LineOne, "Sheets.Add After:=Sheets(Sheets.Count)")
End With
myWorkBook.VBE.MainWindow.Visible = False
Thanks my friend and my problem has solved. Thumbs up
-
May 3rd, 2019, 02:07 AM
#4
Thread Starter
Member
Re: Can you add a macro to Excel worksheet button through VB.NET?
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
|