简体   繁体   中英

Excel-VBA: Adding a button clears the clipboard - workaround needed

OK, so I have some worksheet code that detects when a cell in a range is selected and creates a button alongside it. Everything appeared to be OK, but something about creating the button clears the clipboard. Here's the original code that is called whenever a cell in the relevant range is selected:

Public Sub splitTransactionIn(ByVal Target As Range)
  Dim btn As Button
  Set t = ActiveSheet.Range("E" & Target.Row)
  rowNumber = Target.Row
  On Error Resume Next
  ActiveSheet.Buttons("Split").Delete
  Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
  With btn
    .OnAction = "btnIn"
    .Caption = "Split transaction"
    .Name = "Split"
  End With
End Sub

I tried commenting sections out and inserting breakpoints, and as soon as either .OnAction, .Caption or .Name are reached, the clipboard is cleared. Why, I have no idea.

Thinking about it logically, I added in a few extra lines of code to capture the clipboard before it was cleared and replace it at the end of the sub, so I end up with this:

Public Sub splitTransactionIn(ByVal Target As Range)
  Dim btn As Button
  Dim objData As New MSForms.DataObject
  Dim strText

  objData.GetFromClipboard
  strText = objData.GetText()
  Set t = ActiveSheet.Range("E" & Target.Row)
  rowNumber = Target.Row    ' gets used elsewhere...
  On Error Resume Next
  ActiveSheet.Buttons("Split").Delete
  Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
  With btn
    .OnAction = "btnIn"
    .Caption = "Split transaction"
    .Name = "Split"
  End With
  objData.SetText strText
  objData.PutInClipboard
End Sub

Now, this works, but only to a point. So I select a cell in the range and copy it (Ctrl-C), I now select (say) 3 cells further down in the same range and try to paste (Ctrl-V). What I get is an error message: "Data On Clipboard Is Not The Same Size And Shape As Selected Area" and if I hit OK, it just pastes to a single cell. I know the source and targets are different sizes, but elsewhere in the sheet this works fine when copy/pasting. Clearly I have done something odd in reading/writing the clipboard, but I have no idea what.

OK, so Excel really fought me every step of the way here. In essence, as has been said, any change in the interface wipes the clipboard. So in my case when I selected a target cell, my own code was inserting a new button alongside and wiping the clipboard.

Attempting to copy the clipboard as I tried in my second attempt proved to be very unreliable, for some reason. Anyway, the answer was to use a different column to trigger the button generation, so when I select a target cell for the paste, the clipboard is left intact.

As a final gotcha, be aware that sheet level code to detect when a cell is selected (which is what I was using) doesn't work if the cell is protected...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM