+ Reply to Thread
Results 1 to 9 of 9

Changing Event handler

Hybrid View

  1. #1
    Registered User
    Join Date
    12-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    15

    Question Changing Event handler

    Hi,

    I'm new to vba/vbs and want to know if it is possible to click a button (btnABC) in Excel and go direct to a function in another Module (myfunctionABC) and avoid the default event handler onclick function (btnABC_onclick) for the Sheet to which the button is a member.

    I am restricted from changing the VBA code on the Sheet (so cannot have onclick function point to where I have written my own function, myfunctionABC) - unless I can do that via a vbscript which I run to add sheets and manipulate the Sheet appearance, e.g. add drop down, change cell values etc.

    1) What vbs will inject a function into an existing Sheet,

    e.g.

    private sub btnABC_onclick() 
    
     myfunctionABC()
    
    end sub
    OR

    2) Make button btnABC point directly at myfunctionABC()

    cheers

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Changing Event handler

    Why would you use a button to run a function/

    How can you edit a function, but not the button? Is the function in an addin? If is the button code wouldn't work anyway
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    12-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Changing Event handler

    >> Why would you use a button to run a function/

    I want to call a Procedure/Function - to run a job.

    >> How can you edit a function, but not the button? Is the function in an addin? If is the button code wouldn't work anyway

    It's the standard Excel button. I have a VBscript that currently creates new sheets, adds new buttons, drop downs... but have not found a way how you can create new Procedures in a worksheet from the VBscript, i.e. my own btnABC_Click() so I can make call to myFunctionABC().

  4. #4
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Changing Event handler

    I think it is simpler.

    The event can CALL another function. Say you got a function in Module1 then you can call it by

    call module1.myFunctionABC() if it is a sub or

    if the return type is a string

    MyABCStr = module1.myFunctionABC()

    One can leave the module1 part as the editor will recognize it, but if you have a bigger project I like to include it. Its a personal preference.

    Explination in code
    private sub btnABC_onclick() 
    
     call module1.myfunctionABC()
    
    end sub
    In module1
    sub myfunctionABC()
      'Your code
    end sub
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  5. #5
    Registered User
    Join Date
    12-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Changing Event handler

    This is a spreadsheet provided by a vendor so each time it releases a new copy it overwrites what ever changes are made by myself and that is why the only allowable changes have got to be done via a VBscript build script.

    Can VBscript add those 3 simple lines of VBA code to a target worksheet in an excel file - similar to how it allows me to add a drop down to the front end of the worksheet?

  6. #6
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Changing Event handler

    Sure ...

    Normally this is done as such
    sub myfunctionABC()
    
    Dim Ws as Worksheet
    
    Set Ws = Worksheets("Sheet2")
    
    Ws.Range("A1").value
    'etc
    
    End Sub
    similar to how it allows me to add a drop down to the front end of the worksheet?
    I do not quit understand this one.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,997

    Re: Changing Event handler

    It would, IMO, be easier to use a button from the Forms toolbar (use Buttons.Add) and set its OnAction property to your function rather than using ActiveX buttons and trying to add code to the sheet.
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Registered User
    Join Date
    12-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Changing Event handler

    Cool, thanks.

    In my VBScript I've added this code to add a new Forms button that successfully now calls my function directly. Here is the code: -

    Set= xlWB.Sheets("existingSheet").Buttons.Add(150, 50, 100, 25)
    	newbtn.Characters.Text = "Validate"
    	newbtn.OnAction = "myModule.myFunctionABC"
    	newbtn.Enabled = True
    But how do I delete the existing ActiveX button from VBScript? This is the VBA code but it does not work in VBscript: -

    xlWB.Sheets("existingSheet").Shapes("CommandButton1").Select
        Selection.Cut

  9. #9
    Registered User
    Join Date
    12-15-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Changing Event handler

    Ok problem solved

    xlWB.Sheets("existingSheet").Shapes("CommandButton1").Cut

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1