+ Reply to Thread
Results 1 to 5 of 5

Thread: Code Only Running Manually

  1. #1
    Registered User
    Join Date
    06-15-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2010
    Posts
    3

    Code Only Running Manually

    Hello -

    I'm including the following code within another set of Excel VBA that has worked fine. The purpose of the following code is to remove a formula by pasting special values.

    When I highlight and run all of the code manually, this newly added code component works. When I run the code by clicking the button it's associated with, the original code continues to work fine, but this newly added code pieced does not.

    I don't think it's an Excel worksheet protection issue, as the same issue occurs when I remove all protection.

    Help would be greatly appreciated.

    With Sheet16
        ActiveSheet.Unprotect Password:="password"
        Range("B21:C21").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
             :=False, Transpose:=False
        ActiveSheet.Protect Password:="password"
        Application.CutCopyMode = False    
    End With
    Thanks....
    Last edited by PasteSpecialValues; 06-16-2011 at 10:59 AM.

  2. #2
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    4,004

    Re: Code Only Running Manually

    Hi PasteSpecialValues

    Only guessing without seeing the entire code. You can try modifying the code like this
    With Sheet16
        ActiveSheet.Unprotect Password:="password"
        .Range("B21:C21").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
             :=False, Transpose:=False
        ActiveSheet.Protect Password:="password"
        Application.CutCopyMode = False    
    End With
    If you look carefully, you'll see I changed this line of code by adding a dot (.) before it. This ties the range to the "with" worksheet (Sheet16) and not the active worksheet.
    .Range("B21:C21").Select
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    06-15-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Code Only Running Manually

    John -

    Thanks for your response. When I add the dot before that line a "Run-time error '1004': Select method of Range class failed" message is generated. Any additional thoughts?

    Thanks again,

    Aaron

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Code Only Running Manually

    Hello Aaron,

    Try this method.
    With Sheet16
        .Unprotect Password:="password"
        .Range("B21:C21").Value = .Range("B21:C21").Value
        .Protect Password:="password"
    End With
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    06-15-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Code Only Running Manually

    Leith -

    That worked, thank you so much!

    Aaron

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0