+ Reply to Thread
Results 1 to 10 of 10

Code works in the Module1 but not in the Worksheet?

Hybrid View

  1. #1
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Code works in the Module1 but not in the Worksheet?

    Greetings,
    1- Open enclosed file named HerryMarkowitz1.
    2- Click A1 cell. (When you click A1 cell you will see that everything is okey)
    3- Close the file named HerryMarkowitz1.

    4- Open enclosed file named HerryMarkowitz2.
    5- Click A1 cell. (When you click A1 cell you will see that nothing is okey)

    The question is;
    Why does code work in the Module1 but not in the Worksheet?
    Attached Files Attached Files
    Last edited by HerryMarkowitz; 04-09-2015 at 12:46 PM.
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,868

    Re: Code works in the Module1 but not in the Worksheet?

    Move this macro:
    Sub Macro1()
    MsgBox "Hello"
    End Sub
    from the worksheet code module into a regular module.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Code works in the Module1 but not in the Worksheet?

    Quote Originally Posted by Mumps1 View Post
    Move this macro:
    Sub Macro1()
    MsgBox "Hello"
    End Sub
    from the worksheet code module into a regular module.
    I know that...
    My question is why does code work in the Module1 but not in the Worksheet? I want the code can work in the Worksheet...

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,868

    Re: Code works in the Module1 but not in the Worksheet?

    Check out this link. It might explain the reason. http://www.ozgrid.com/forum/showthread.php?t=87159

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Code works in the Module1 but not in the Worksheet?

    Does this help?

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, [A1]) Is Nothing Then MsgBox "Hello"
    End Sub

  6. #6
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Code works in the Module1 but not in the Worksheet?

    Quote Originally Posted by JOHN H. DAVIS View Post
    Does this help?
    Hi John
    It doesnt help...
    Because my question is regarding how to run other SubRoutine in the WorkSheet Module...

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Code works in the Module1 but not in the Worksheet?

    You can call it.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, [A1]) Is Nothing Then Call MacroOtherSubRoutine
    End Sub
    Will call your macro upon changes made to A1.

  8. #8
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Code works in the Module1 but not in the Worksheet?

    I have put following codes into Sheet1 code module but my problem havent been solved yet...
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, [A1]) Is Nothing Then Call Macro1
    End Sub
    Sub Macro1()
    MsgBox "Hello"
    End Sub

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code works in the Module1 but not in the Worksheet?

    Macro1 shouldn't really be in a worksheet(or workbook) module.

    The only code that should be in such a module should be code that is associated with the worksheet/workbook.

    For example code for a SelectionChange/Change event.
    If posting code please use code tags, see here.

  10. #10
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Code works in the Module1 but not in the Worksheet?

    Quote Originally Posted by Norie View Post
    Macro1 shouldn't really be in a worksheet(or workbook) module.
    The only code that should be in such a module should be code that is associated with the worksheet/workbook.
    For example code for a SelectionChange/Change event.
    Hi Norie,
    I am really confused...
    Please put following code into Sheet1 code module then see working code properly...
    Private Sub Worksheet_Activate()
    Call Macro1
    End Sub
    Sub Macro1()
    MsgBox "Hello"
    End Sub

+ 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. ToggleButton1_Click doesnt work if code is located Module1!
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2015, 03:56 PM
  2. Code fails when ran within Userform, but works when ran from Macro button on worksheet.
    By Nitefox in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 6
    Last Post: 11-29-2013, 02:28 AM
  3. [SOLVED] Code Gives error if I run it from Module but works fine when run it from This Worksheet
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-17-2013, 01:18 PM
  4. [SOLVED] Excel 2010+ Workbook- running Module1 code – Not Responding
    By xladept in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 05-13-2013, 04:57 PM
  5. VBA Code works but not on worksheet
    By elabounty in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-14-2011, 03:00 PM

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