+ Reply to Thread
Results 1 to 8 of 8

how to click a button from the ThisWorkbook module

Hybrid View

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    how to click a button from the ThisWorkbook module

    how can i click a button on one of my sheets from the ThisWorkbook module?

    i have tried the following:

    call commandbutton1_click
    call sheet1.commandbutton1
    call sheet1.commandbutton1_click
    application.run worksheets(sheet1).commandbutton1_click
    application.run worksheets("check request").commandbutton1_click
    nothing seems to work.

    what am i doing wrong?

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: how to click a button from the ThisWorkbook module

    If you are talking about an ActiveX command button, in the sheet module, declare the CommandButton1_Click as a Public sub.

    Public Sub CommandButton1_Click()
        ' do stuff
    End Sub
    In the ThisWorkbook module use code like

    '...
    
    Sheet1.CommandButton1_Click
    
    '...
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to click a button from the ThisWorkbook module

    I would suggest that if you need to do this, the code should be in a normal module and visible to both the sheet's button and the ThisWorkbook code. If you do wish to do it using Run the syntax is
    application.run "sheet1.commandbutton1_click"
    assuming that sheet1 is the code name of the relevant sheet.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: how to click a button from the ThisWorkbook module

    I would have thought that if the sub "Commandbutton_click" is a public sub in a normal module, you can call it from any worksheet or other module by just Commandbutton_click and not have to do the Application.run with the sheet1 qualifier.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: how to click a button from the ThisWorkbook module

    Correct- my comment about Run was assuming the current code position.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: how to click a button from the ThisWorkbook module

    The CommandButton1_Click event code has to be in the sheet's code module for the button to trigger it, you could do

    ' in Sheet module
    
    Sub CommandButton1_Click()
        Call ButtonRoutine
    End Sub
    ' in normal module
    
    Sub ButtonRoutine()
        ' do stuff
    End Sub
    ' in ThisWorkbook
    
    '...
    
    Call ButtonRoutine
    
    '...

  7. #7
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    273

    Re: how to click a button from the ThisWorkbook module

    Can't you just put the CommandButton1_Click () sub in a general module and assign that macro to the button?

  8. #8
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: how to click a button from the ThisWorkbook module

    @Mik

    Your second solution works everytime. the first one worked one time, then failed on subsequent opens.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. ThisWorkbook Module Add-in
    By asalamun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-27-2017, 01:04 PM
  2. [SOLVED] Can't reference ThisWorkbook from a module in another workbook module
    By Bukovnik in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2015, 11:32 AM
  3. Help calling a module from a button click
    By jpalmos in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-24-2013, 03:16 PM
  4. Replies: 1
    Last Post: 08-30-2011, 02:23 AM
  5. [SOLVED] Spin Button control change capture in ThisWorkbook Module
    By STEVE BELL in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-12-2005, 05:05 PM
  6. [SOLVED] How to click the button from module?
    By OKLover in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-07-2005, 06:05 PM
  7. [SOLVED] Calls from sheet module to ThisWorkbook module
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2005, 11:05 AM

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