SolvedPrivate

Called subroutine within macro does not function except in Debug / F8 Step Into mode

Posted on 2015-02-10
9
27 Views
Last Modified: 2016-02-11
Hello Experts,
In the following macro the five "CallClearPlan 1 to 5 routines  when run do not clear the 5 plans and the macro ends properly.  When I Debug using F8 the macro steps through each of the 5 called Subs and does clear all fields in all 5 plans.  
How to correct this poor behavior is my question?


Sub ClearResponsesAndPlans()
'
'
'

    Response = MsgBox(prompt:="Select 'Yes' if you want to clear all 5 plans  'No' to quit.", Buttons:=vbYesNo + vbInformation)
    ' If statement to check if the yes button was selected.
  If Response = vbYes Then
        MsgBox "You selected 'Yes' to clear each of 5  Plans", vbExclamation, "Clear  Macro"
       
        Call ClearPlan1    
        Call ClearPlan2
        Call ClearPlan3
        Call ClearPlan4
        Call ClearPlan5
       
    Else
         ' The no button was selected.
         MsgBox "You selected 'No'. Press 'OK' to quit", vbCritical, "  Quit Macro "
       
  End If
 
        Sheets("Assessment").Select
        ActiveWindow.SmallScroll Down:=-87
        Range("Assess_Responses").Select
   
End Sub
~-~-~-~-~-~-~-~-~-
Below is a one of the five typical Called sub routines each of which function properly from individual commandbuttons...
~-~-~-~-~-~-~-~-~-
Sub ClearPlan1()
'
' ClearPlan1 Macro
'
'
    Range( _
        "B26:P30,B33:P37,D41:M43,D44:M46,D47:M49,D50:M52,N41:P43,N44:P46,N47:P49" _
        ).Select
   
    Selection.ClearContents
    Range("B8").Activate
End Sub


Thank you
0
Comment
Question by:Berry Metzger
  • 4
  • 4
9 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 40600492
Which sheet are the ranges you want to clear on?
0
 
LVL 50

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 total points
ID: 40600495
Hi,

Are your plans  in different sheets?

Regards
0
 

Author Comment

by:Berry Metzger
ID: 40600539
all of the plans are on one sheet:  "Action Plans"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Accepted Solution

by:
Norie earned 250 total points
ID: 40600544
Do the subs work if you specify that sheet?
Sub ClearPlan1()
'
' ClearPlan1 Macro
'
'
   Sheets( "Action Plans").Range("B26:P30,B33:P37,D41:M43,D44:M46,D47:M49,D50:M52,N41:P43,N44:P46,N47:P49" ).ClearContents
    
End Sub

Open in new window

0
 

Author Comment

by:Berry Metzger
ID: 40600549
yes they do when I invoke each one using 5 separate command buttons on the same sheet.
0
 
LVL 33

Expert Comment

by:Norie
ID: 40600575
Sorry I don't follow.

If you change the code for each sub to include the sheet reference, as in the example I posted, then the code should work wherever/however it's called.

In the original code there was no sheet reference so the code would be executed on whichever sheet was active at the tim.
0
 

Author Comment

by:Berry Metzger
ID: 40600587
I awarded both of you points because both of your questions arrived just two minutes apart led me to the answer that had escaped me.  By adding a Sheets"Select "Action Plans".select statement it now works fine when called from another sheet.
Thanks for your guidance.
Berry
0
 
LVL 33

Expert Comment

by:Norie
ID: 40600609
Berry

There's no need to Select/Activate and doing so can cause problems, eg subsequent code running on wrong sheet.

You can just add the sheet reference as I did in the code I posted.
0
 

Author Comment

by:Berry Metzger
ID: 40600738
Thank you for again clearing up my error
Berry
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question