+ Reply to Thread
Results 1 to 7 of 7

Turning on automatic calculation clears the clipboard

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Turning on automatic calculation clears the clipboard

    I’m rewriting some existing Worksheet_SelectionChange code to make it run fast based on things I’ve learned here. Below is the shell of my new module. I’ve noticed a problem with the Select Case statement as I started to build it. I’ve added a line to set the calculation to manual at the beginning and back to auto at the end of the module. That is causing my problem. When the auto calculation is turned back on, it clears the clipboard, so I lose the copy command.
    Anyone have any suggestions? Also, does anyone know how/if you can test in VBA whether a range has a defined name? I thought if I perhaps I could do something like:
    If Target.name.name =”” then Target.name.name= “unnamed”
    But that either can’t be done or the syntax is wrong.
    Thanks for any suggestions.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo Errorhandler
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    Select Case Target.Name.Name
        Case "SubmitTime"
            Range("SubmitTime").Copy
    End Select
    
    Errorhandler:
    With Application
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Turning on automatic calculation clears the clipboard

    Your routine doesn't make sense to me, but why not just:

    Range("SubmitTime").Copy
    In any wrapper?
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Turning on automatic calculation clears the clipboard

    I want it to "automatically" copy that specific cell when I click on it - one action. Of course I could do the same thing with a separate macro that I could invoke with the click of a macro button - again one action. But ultimately I will have other named-ranges to copy and rather than cluttering up my worksheet with a separate button for each one, I found this approach is cleaner. Name the range, add it to the Select Case with the appropriate "copy" command and when you want to copy a cell you just select it with your mouse. However my original code did not include .Calculation = xlCalculationManual / .Calculation = xlCalculationAutomatic. Adding that defeats the original copy code as written. I can take the xlCalculationManual / Automatic back out, but there seems like there should be a better way to do it. Alternatively, I could add a msgbox right before the xlCalculationAutomatic code would would allow me to paste the "SubmitTime" (into another application) before clicking the "ok" to allow the VBA to finish, but that seems like a lazy way to do it. Let me know if you have any other questions - thanks for looking at my code.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Turning on automatic calculation clears the clipboard

    But if you're just copying a named range - why does the calculation mode matter?

  5. #5
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Turning on automatic calculation clears the clipboard

    That's not all it will do - Also I have the same problem with Worksheet_Change with the the same cell. That specific range actually has a drop-down box data validation associated with it. So I can select it and it copies OR if I want to change the time, I can pick it from the drop-down menu and it copies the new value. Here is the current code in its entirety (although I'm also expanding it with this rewrite.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo ErrorHandler
    If ActiveSheet.Shapes("check box 5").ControlFormat.Value = -4146 Then
        RngName = ActiveCell.Name.Name
            Select Case RngName
                Case "LinkSnapshot"
                    sWebsiteVal = Sheets("Maintenance").Range("SnapshotLink").Text
                    ActiveWorkbook.FollowHyperlink Address:=sWebsiteVal, NewWindow:=True 'Open Website
                Case "HoldingLink"
                    sWebsiteEE = Sheets("Maintenance").Range("NasdaqHoldingLink").Text
                    ActiveWorkbook.FollowHyperlink Address:=sWebsiteEE, NewWindow:=True 'Open Website
                Case "SubmitTime", "EarningsDate"
                    Selection.Copy
            End Select
        StratNo = ActiveSheet.Range("StratChoice").Value
        If RngName = "jMove_to_Entry" Then
            Application.EnableEvents = False
            Select Case StratNo
                Case 1, 2
                    Application.GoTo reference:=ActiveCell.Offset(19, 0)
                Case 3, 4
                    Application.GoTo reference:=ActiveCell.Offset(54, 0)
                Case 5, 6
                    Application.GoTo reference:=ActiveCell.Offset(54, 0)
                Case 7, 8
                    Application.GoTo reference:=ActiveCell.Offset(120, 0)
            End Select
            Application.EnableEvents = True
        Else
            Application.EnableEvents = False
            If RngName Like "jStop*" Then RngName = "Stop"
            If RngName Like "jTimeStop*" Then RngName = "TimeStop"
            If RngName Like "jOptRisk*" Then RngName = "OptRisk"
            If RngName Like "jEst_Tgt_Price*" Then RngName = "Est_Tgt_Price"
                Select Case RngName
                    Case Is = "Stop"
                        Select Case StratNo
                            Case 1, 2
                                Application.GoTo reference:=ActiveCell.Offset(17, -3)
                            Case 5, 6, 7, 8
                                Application.GoTo reference:=ActiveCell.Offset(15, -3)
                        End Select
                    Case Is = "TimeStop"
                        Application.GoTo reference:=ActiveCell.Offset(1, 0)
                    Case Is = "OptRisk"
                        Application.GoTo reference:=ActiveCell.Offset(1, 0)
                    Case Is = "Est_Tgt_Price"
                        Application.GoTo reference:=ActiveCell.Offset(5, -3)
                    Case Is = "j80__of_Move"
                        Application.GoTo reference:=ActiveCell.Offset(17, -3)
                End Select
            Application.EnableEvents = True
            End If
    End If
    ErrorHandler:
    End Sub
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Exitcount As Long
    Application.ScreenUpdating = False
    
    On Error GoTo Errhandler
    
    If ActiveSheet.Shapes("check box 11").ControlFormat.Value = 1 Then
        Application.EnableEvents = False
        ActiveCell.Offset(-1, 0).Select
        Application.EnableEvents = True
       ' If ActiveCell.Name.Name = "Symbol" Then ActiveSheet.Range("EarningsDate").Copy
    End If
    
    If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value + _
    ActiveSheet.Shapes("Check Box 2").ControlFormat.Value + _
    ActiveSheet.Shapes("Check Box 3").ControlFormat.Value + _
    ActiveSheet.Shapes("Check Box 4").ControlFormat.Value > -16584 Then
        OptTrade = Range("Opt_Type_Strat1").Value + Range("Opt_Type_Strat2").Value + _
        Range("Opt_Type_Strat3").Value + Range("Opt_Type_Strat4").Value
        Select Case OptTrade
            Case 1, 2
                Application.EnableEvents = False
                ActiveCell.Offset(0, 1).Select
                ActiveCell.Offset(1, -5).Select
                ActiveWindow.ScrollColumn = 1
                ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = -4146
                ActiveSheet.Shapes("Check Box 2").ControlFormat.Value = -4146
                ActiveSheet.Shapes("Check Box 3").ControlFormat.Value = -4146
                ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = -4146
            Case 3, 11, 21
                Application.EnableEvents = False
                If Range("Opt_Type_Strat1").Value <> "" Then Range("OptSelect1a").Select
                If Range("Opt_Type_Strat2").Value <> "" Then Range("OptSelect2a").Select
                If Range("Opt_Type_Strat3").Value <> "" Then Range("OptSelect3a").Select
                If Range("Opt_Type_Strat4").Value <> "" Then Range("OptSelect4a").Select
                ActiveCell.Offset(0, 3).Activate
        End Select
    End If
    If ActiveCell.Column = 11 And _
    Range("jOpt_PP1").Value + Range("jOpt_PP2").Value + _
    Range("jOpt_PP3").Value + Range("jOpt_PP4").Value <> 0 Then
        Application.EnableEvents = False
        ActiveCell.Offset(0, 1).Select
        ActiveCell.Offset(1, -8).Select
        ActiveWindow.ScrollColumn = 1
        ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = -4146
        ActiveSheet.Shapes("Check Box 2").ControlFormat.Value = -4146
        ActiveSheet.Shapes("Check Box 3").ControlFormat.Value = -4146
        ActiveSheet.Shapes("Check Box 4").ControlFormat.Value = -4146
        ActiveWindow.ScrollColumn = 1
    End If
    
    Application.EnableEvents = False
    
    If ActiveSheet.Range.Name.Name = "SubmitTime" Then Selection.Copy
    
    Errhandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Turning on automatic calculation clears the clipboard

    Nevermind - I figured it out - finally hit upon an search led me to a clue. I changed the error handling from On Error GoTo to ON Error Resume Next and added the following code first:
    If Target.Name.Name = "unnamed" Then
            If Err.Number = 1004 Then Exit Sub
    End If
    Now the first thing it does is test for a defined name. Then I just made the last line "If Target.Name.Name = "SubmitTime" Then Selection.Copy" - AFTER have the AutoCalc turned back on - works like a charm. Thanks for your time.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Turning on automatic calculation clears the clipboard

    You're welcome - glad you got it!

+ 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. Excel clipboard clears
    By RealQuickQuestion in forum Excel General
    Replies: 3
    Last Post: 11-25-2009, 06:51 PM
  2. Running macros/events clears undo history and clipboard
    By Creisti86 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-05-2007, 10:33 AM
  3. [SOLVED] Strange behaviour - loading Excel clears Clipboard
    By Trevor Shuttleworth in forum Excel General
    Replies: 1
    Last Post: 08-19-2006, 05:10 PM
  4. Add method of SeriesCollection object clears Windows clipboard
    By Richo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-02-2006, 11:30 AM
  5. ClearContents or ClearFormats also clears Clipboard. How can I keep the clipboard?
    By rlaemmler@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-15-2005, 11:35 PM
  6. [SOLVED] turning off automatic calculation when loading VBA add-in
    By Lars Schouw in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2005, 02:05 AM
  7. [SOLVED] Turning off automatic calculation when opening a CSV file
    By Will in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2005, 06:05 AM
  8. Worksheet_Activate event clears clipboard?
    By Andibevan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-07-2005, 11:05 AM

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.6.0 RC 1