PC Review


Reply
Thread Tools Rate Thread

Creating a procedure using VBA Code

 
 
kjamison@travelers.com
Guest
Posts: n/a
 
      5th Feb 2008
I am using an Excel/VBA application to read SQL databases - then
create a pivot table using that data and put it on a new workbook.

Now I am trying to programmatically add a command button to a sheet on
that new workbook and put code behind it (meaning adding a SUB
procedure to the workbook as well, that is executed by a Click event
on the new command button).

I have the button added and renamed - now I am trying to figure out
how to add a Sub Procedure and then the Click event on that command
button.

Any suggestions??
 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      5th Feb 2008
Here is an example

Dim StartLine As Long

With ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
StartLine = .CreateEventProc("Click", "CommandButton1") + 1
.InsertLines StartLine, _
"Dim ans" & vbCrLf & _
" ans = Msgbox( ""All OK"",vbYesNo)" & vbCrLf & _
" If ans = vbNo Then Cancel = True"
End With



--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



<(E-Mail Removed)> wrote in message
news:1650ad93-f01c-44b5-a6be-(E-Mail Removed)...
>I am using an Excel/VBA application to read SQL databases - then
> create a pivot table using that data and put it on a new workbook.
>
> Now I am trying to programmatically add a command button to a sheet on
> that new workbook and put code behind it (meaning adding a SUB
> procedure to the workbook as well, that is executed by a Click event
> on the new command button).
>
> I have the button added and renamed - now I am trying to figure out
> how to add a Sub Procedure and then the Click event on that command
> button.
>
> Any suggestions??



 
Reply With Quote
 
kjamison@travelers.com
Guest
Posts: n/a
 
      6th Feb 2008
On Feb 5, 3:21*pm, "Bob Phillips" <bob....@somewhere.com> wrote:
> Here is an example
>
> Dim StartLine As Long
>
> * * With ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> * * * * StartLine = .CreateEventProc("Click", "CommandButton1") + 1
> * * * * .InsertLines StartLine, _
> * * * * "Dim ans" & vbCrLf & _
> * * * * " * ans = Msgbox( ""All OK"",vbYesNo)" & vbCrLf & _
> * * * * " * If ans = vbNo Then Cancel = True"
> * * End With
>
> --
> ---
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
> <kjami...@travelers.com> wrote in message
>
> news:1650ad93-f01c-44b5-a6be-(E-Mail Removed)...
>
>
>
> >I am using an Excel/VBA application to read SQL databases - then
> > create a pivot table using that data and put it on a new workbook.

>
> > Now I am trying to programmatically add a command button to a sheet on
> > that new workbook and put code behind it (meaning adding a SUB
> >procedureto the workbook as well, that is executed by a Click event
> > on the new command button).

>
> > I have the button added and renamed - now I am trying to figure out
> > how to add a SubProcedureand then the Click event on that command
> > button.

>
> > Any suggestions??- Hide quoted text -

>
> - Show quoted text -


THANKS - worked like a charm.
 
Reply With Quote
 
kjamison@travelers.com
Guest
Posts: n/a
 
      6th Feb 2008
On Feb 5, 3:21*pm, "Bob Phillips" <bob....@somewhere.com> wrote:
> Here is an example
>
> Dim StartLine As Long
>
> * * With ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> * * * * StartLine = .CreateEventProc("Click", "CommandButton1") + 1
> * * * * .InsertLines StartLine, _
> * * * * "Dim ans" & vbCrLf & _
> * * * * " * ans = Msgbox( ""All OK"",vbYesNo)" & vbCrLf & _
> * * * * " * If ans = vbNo Then Cancel = True"
> * * End With
>
> --
> ---
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
> <kjami...@travelers.com> wrote in message
>
> news:1650ad93-f01c-44b5-a6be-(E-Mail Removed)...
>
>
>
> >I am using an Excel/VBA application to read SQL databases - then
> > create a pivot table using that data and put it on a new workbook.

>
> > Now I am trying to programmatically add a command button to a sheet on
> > that new workbook and put code behind it (meaning adding a SUB
> >procedureto the workbook as well, that is executed by a Click event
> > on the new command button).

>
> > I have the button added and renamed - now I am trying to figure out
> > how to add a SubProcedureand then the Click event on that command
> > button.

>
> > Any suggestions??- Hide quoted text -

>
> - Show quoted text -


OK - works like a charm - but opens up the VBE Project window - how do
you close that programatically??
 
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
Creating Stored Procedure Terrance Microsoft ADO .NET 5 13th Jan 2008 06:48 AM
invalid outside procedure when creating a rs =?Utf-8?B?SmFuaXM=?= Microsoft Access VBA Modules 3 15th Sep 2007 01:20 PM
Creating event procedure on worksheet Dantz Microsoft Excel Programming 0 1st Dec 2005 01:00 AM
Creating An Event Procedure Les Stout Microsoft Excel Programming 4 4th Oct 2005 02:03 PM
Help creating a stored procedure to call from .Net RSH Microsoft VB .NET 1 16th Aug 2005 06:26 PM


Features
 

Advertising
 

Newsgroups
 


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