Consulting

Results 1 to 4 of 4

Thread: Solved: Code is clearing clipboard

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Solved: Code is clearing clipboard

    I'm using the following code to highlight row and column headers. When I try to copy and paste, the clipboard is being emptied, so no Paste is available. Any thoughts?
    [VBA]
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Columns(3).Interior.ColorIndex = xlNone
    Rows(2).Interior.ColorIndex = xlNone
    Cells(Target.Row, 3).Interior.ColorIndex = 6
    Cells(2, Target.Column).Interior.ColorIndex = 6
    End Sub

    [/VBA]
    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'

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    I don't think you can have both - you could check Application.CutCopyMode and skip the colouring in if there's a cut/copy in progress?
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    On second thought, if you set a reference to the Microsoft Forms library, you could try something like this:
    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim objData As MSForms.DataObject
    If Target.Cells.Count > 1 Then Exit Sub
    If Application.CutCopyMode = xlCopy Or Application.CutCopyMode = xlCut Then
    Set objData = New MSForms.DataObject
    objData.GetFromClipboard
    End If
    Columns(3).Interior.ColorIndex = xlNone
    Rows(2).Interior.ColorIndex = xlNone
    Cells(Target.Row, 3).Interior.ColorIndex = 6
    Cells(2, Target.Column).Interior.ColorIndex = 6
    If Not objData Is Nothing Then objData.PutInClipboard
    End Sub
    [/vba]
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Works a treat.
    Thanks Rory.
    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'

Posting Permissions

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