Consulting

Results 1 to 6 of 6

Thread: Solved: How do you run ThisWorkbook code from a module?

  1. #1

    Question Solved: How do you run ThisWorkbook code from a module?

    I am trying to run some code contain in ThisWorkBook under the sub Workbook_SheetSelectionChange. I am trying to use some code in module1 to toggle the ThisWorkbook code on and off when the user select that option under the contextmenu.

    Where am I going wrong with the code below?

    Module1 Code:
    [vba]
    Public bSwitch As Boolean
    Public bRw As Boolean

    Sub MyTestCode()

    'Enable user to switch on/off with a right click prompt
    If bSwitch Then
    If MsgBox("Shut off", 36) = 7 Then Exit Sub

    Else
    If MsgBox("Turn on", 36) = 7 Then

    ThisWorkbook.Test

    End If

    'Toggle boolean variable on/off switch
    If Selection.Rows.Count > 1 Then
    bRw = False
    Else
    bRw = True
    End If
    bSwitch = Not bSwitch

    End If
    End Sub
    [/vba]
    ThisWorkbook Code:
    [vba]
    Public Sub Test()
    If Not bSwitch Then Exit Sub
    Else
    ThisWorkbook.Workbook_SheetSelectionChange
    End If
    End Sub


    Public Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    'If we are running procedure or not:
    If Not bSwitch Then Exit Sub

    ' More code is located here removed for posting.

    End Sub
    [/vba]
    Last edited by nousername; 03-23-2008 at 08:58 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Two things.

    Code like this

    [vba]

    If Not bSwitch Then Exit Sub
    Else
    ThisWorkbook.Workbook_SheetSelectionChange
    End If
    [/vba]

    is just plain wrong, you are mixing a single line If with an If ... Else structure. It should be

    [vba]
    If Not bSwitch Then
    Exit Sub
    Else
    ThisWorkbook.Workbook_SheetSelectionChange
    End If
    [/vba]

    You do this in two places.

    Secondly, the event procedure you are calling has arguments, so you must pass parameter values to the procedure arguments for it to run.
    Last edited by Bob Phillips; 03-23-2008 at 04:48 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Nousername,
    When you post code here, select it and click the VBA button to format it as shown. The Code tags don't do that
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Here is a sample project of what I am trying to do.

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    HI nn,

    and here's your sample back... I think you were trying too hard? <g>

    Dusty
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  6. #6
    YOU ROCK!! The code works just like I wanted it too!!

    THANKS!!

Posting Permissions

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